gpt4 book ai didi

php - 组合或交叉多个 MySQL 命令

转载 作者:行者123 更新时间:2023-11-29 11:02:52 26 4
gpt4 key购买 nike

我必须搜索两个表。

我的第二个表是:

+---------+---------------+-------+
| user_id | is_first_time | score |
+---------+---------------+-------+
| 44 | 1 | 20 |
+---------+---------------+-------+
| 44 | 0 | 20 |
+---------+---------------+-------+

我的第一个表仅包含用户信息。

  • 如果我使用以下 MySQL 命令,我将得到结果:

    SELECT * , users_score.user_id users_score_user_id 
    FROM users JOIN users_score
    ON users_score.user_id = users.id
    WHERE (users_score.score='20' AND users_score.is_first_time='1')
  • 如果我使用以下 MySQL 命令,我也会得到结果(我只需将“is_first_time”值更改为“0”:

    SELECT * , users_score.user_id users_score_user_id 
    FROM users JOIN users_score
    ON users_score.user_id = users.id
    WHERE (users_score.score='20' AND users_score.is_first_time='0')
  • 但是如果我将这两个命令与 AND 结合起来,我将不会得到任何结果:

    SELECT * , users_score.user_id users_score_user_id 
    FROM users JOIN users_score
    ON users_score.user_id = users.id
    WHERE (users_score.score='20' AND users_score.is_first_time='1')
    AND (users_score.score='20' AND users_score.is_first_time='0')

如何组合或交叉这两个命令?

感谢您的帮助。

最佳答案

您需要在此处使用OR条件

WHERE (users_score.score='20' AND users_score.is_first_time='1') 
OR (users_score.score='20' AND users_score.is_first_time='0')

但是可以使用 IN 条件以更好的方式编写

SELECT *,
users_score.user_id users_score_user_id
FROM users
JOIN users_score
ON users_score.user_id = users.id
WHERE users_score.score = '20'
AND users_score.is_first_time IN ( '1', '0' )

关于php - 组合或交叉多个 MySQL 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41921195/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com