组合或交叉多个 MySQL 命令
Combine or Intersect Multiple MySQL Commands
我必须搜索两个 table。
我的第二个table是:
+---------+---------------+-------+
| user_id | is_first_time | score |
+---------+---------------+-------+
| 44 | 1 | 20 |
+---------+---------------+-------+
| 44 | 0 | 20 |
+---------+---------------+-------+
我的第一个table只包含用户信息。
如果我使用下面的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' )
如果您希望用户同时满足两个条件,则一种方法使用 in
或 exists
:
SELECT u.*
FROM users u
WHERE u.id IN (SELECT us.user_id FROM users_score us WHERE us.score = '20' AND us.is_first_time = '0'
) AND
u.id IN (SELECT us.user_id FROM users_score us WHERE us.score = '20' AND us.is_first_time = '1'
);
AND
的问题是单行不能同时为 0 和 1。
如果你只想要用户 ID,我会推荐 group by
和 having
:
select us.user_id
from users_score us
where score = 20 and us.is_first_time in (0, 1)
group by us.user_id
having count(distinct us.is_first_time) = 2;
请注意,我删除了单引号。如果列是数字,则不要使用单引号进行比较。 (当然,如果是字符串,就用单引号)
我必须搜索两个 table。
我的第二个table是:
+---------+---------------+-------+
| user_id | is_first_time | score |
+---------+---------------+-------+
| 44 | 1 | 20 |
+---------+---------------+-------+
| 44 | 0 | 20 |
+---------+---------------+-------+
我的第一个table只包含用户信息。
如果我使用下面的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' )
如果您希望用户同时满足两个条件,则一种方法使用 in
或 exists
:
SELECT u.*
FROM users u
WHERE u.id IN (SELECT us.user_id FROM users_score us WHERE us.score = '20' AND us.is_first_time = '0'
) AND
u.id IN (SELECT us.user_id FROM users_score us WHERE us.score = '20' AND us.is_first_time = '1'
);
AND
的问题是单行不能同时为 0 和 1。
如果你只想要用户 ID,我会推荐 group by
和 having
:
select us.user_id
from users_score us
where score = 20 and us.is_first_time in (0, 1)
group by us.user_id
having count(distinct us.is_first_time) = 2;
请注意,我删除了单引号。如果列是数字,则不要使用单引号进行比较。 (当然,如果是字符串,就用单引号)