MySql 如何结合 LEFT JOIN 查询和 NOT IN 查询
MySql how to combine LEFT JOIN query with NOT IN query
我需要将 LEFT JOIN
查询与 NOT IN
查询结合起来,并且必须从这些查询中获取结果值。我在分别使用这两个查询时得到了正确的结果。
我有两个 table 可能 user
和 answer
user
table
user_id
1
2
3
4
5
answer
table
user_id date
1 2015-10-15 21:23:14
2 2015-10-15 20:23:14
3 2015-11-11 16:23:14
LEFT JOIN
查询:
SELECT user.user_id
FROM user
LEFT JOIN answer
ON user.user_id = answer.user_id
WHERE DATEDIFF(NOW(),answer.date) > 5
本次查询return结果user_id
1,2.
NOT IN
查询:
SELECT user.user_id
FROM user
WHERE user.user_id NOT IN (SELECT answer.user_id FROM answer)
本次查询return的结果user_ids
4, 5.
我需要将这两个查询组合成一个查询,所以我尝试了以下两个查询:
SELECT user.user_id
FROM user
LEFT JOIN answer
ON user.user_id = answer.user_id
WHERE (DATEDIFF(NOW(),answer.date) > 5
AND user.user_id NOT IN (SELECT answer.user_id FROM answer))
和
SELECT user.user_id
FROM user
LEFT JOIN answer
ON user.user_id = answer.user_id
WHERE user.user_id NOT IN (SELECT answer.user_id FROM answer)
AND DATEDIFF(NOW(),answer.date) > 5
但是这些return是空的user_id
。
编辑
预期结果应包含 1,2,4,5
的值
如果您的意思是将 2 个选择组合在一起
尝试 Union All / Union :
SELECT * FROM
(
SELECT [WITH YOUR LEFT JOIN STATEMENT]
UNION
SELECT [WITH YOUR NOT IN STATEMENT]
) ResultTABLE
如果您参考 WHERE
子句中的答案 table,answer.date
将被限制为非 NULL 值,使 LEFT JOIN
表现得像定期加入。将条件移动到 ON
条件以检索 all 用户,加上 maybe answers
中的匹配行:
SELECT user.user_id
FROM user u
LEFT JOIN answer a ON u.user_id = a.user_id
AND DATEDIFF(NOW(),a.date) < 5
;
编辑:在问题编辑之后,OP 似乎想要加入不存在答案的记录或 answer.date old/young:
SELECT user.user_id
FROM user u
LEFT JOIN answer a ON u.user_id = a.user_id
WHERE a.date IS NULL -- no answer record
OR DATEDIFF(NOW(),a.date) > 5 -- too old answer record
;
最终版本:由于 OP 想要找到没有(最近)答案的用户,查询可以简化为:
SELECT user.user_id
FROM user u
WHERE NOT EXISTS (
SELECT * FROM answer a
WHERE a.user_id = u.user_id
AND DATEDIFF(NOW(), a.date) <= 5
);
我需要将 LEFT JOIN
查询与 NOT IN
查询结合起来,并且必须从这些查询中获取结果值。我在分别使用这两个查询时得到了正确的结果。
我有两个 table 可能 user
和 answer
user
table
user_id
1
2
3
4
5
answer
table
user_id date
1 2015-10-15 21:23:14
2 2015-10-15 20:23:14
3 2015-11-11 16:23:14
LEFT JOIN
查询:
SELECT user.user_id
FROM user
LEFT JOIN answer
ON user.user_id = answer.user_id
WHERE DATEDIFF(NOW(),answer.date) > 5
本次查询return结果user_id
1,2.
NOT IN
查询:
SELECT user.user_id
FROM user
WHERE user.user_id NOT IN (SELECT answer.user_id FROM answer)
本次查询return的结果user_ids
4, 5.
我需要将这两个查询组合成一个查询,所以我尝试了以下两个查询:
SELECT user.user_id
FROM user
LEFT JOIN answer
ON user.user_id = answer.user_id
WHERE (DATEDIFF(NOW(),answer.date) > 5
AND user.user_id NOT IN (SELECT answer.user_id FROM answer))
和
SELECT user.user_id
FROM user
LEFT JOIN answer
ON user.user_id = answer.user_id
WHERE user.user_id NOT IN (SELECT answer.user_id FROM answer)
AND DATEDIFF(NOW(),answer.date) > 5
但是这些return是空的user_id
。
编辑
预期结果应包含 1,2,4,5
如果您的意思是将 2 个选择组合在一起
尝试 Union All / Union :
SELECT * FROM
(
SELECT [WITH YOUR LEFT JOIN STATEMENT]
UNION
SELECT [WITH YOUR NOT IN STATEMENT]
) ResultTABLE
如果您参考 WHERE
子句中的答案 table,answer.date
将被限制为非 NULL 值,使 LEFT JOIN
表现得像定期加入。将条件移动到 ON
条件以检索 all 用户,加上 maybe answers
中的匹配行:
SELECT user.user_id
FROM user u
LEFT JOIN answer a ON u.user_id = a.user_id
AND DATEDIFF(NOW(),a.date) < 5
;
编辑:在问题编辑之后,OP 似乎想要加入不存在答案的记录或 answer.date old/young:
SELECT user.user_id
FROM user u
LEFT JOIN answer a ON u.user_id = a.user_id
WHERE a.date IS NULL -- no answer record
OR DATEDIFF(NOW(),a.date) > 5 -- too old answer record
;
最终版本:由于 OP 想要找到没有(最近)答案的用户,查询可以简化为:
SELECT user.user_id
FROM user u
WHERE NOT EXISTS (
SELECT * FROM answer a
WHERE a.user_id = u.user_id
AND DATEDIFF(NOW(), a.date) <= 5
);