我正在尝试使用子查询编写SQL SELECT语句。没有返回错误,但我没有得到我期待的结果。 r.related
的值始终为NULL。
SELECT
l.id,
u.id as user_id,
u.name,
r.related
FROM
list l
INNER JOIN user u ON u.id = l.user_id
LEFT JOIN (
SELECT COUNT(u.id) AS related, b.group_id
FROM user u
INNER JOIN booking b ON b.user_id = u.id
WHERE u.id != l.user_id
AND b. = 0) AS r ON r.group_id = l.group_id
WHERE
l.group_id = 22
GROUP BY l.id, u.id
ORDER BY l.id
我正在编写子查询吗?
答案 0 :(得分:0)
问题在于:
SELECT COUNT(u.id) AS related, b.group_id
FROM user u
INNER JOIN booking b ON b.user_id = u.id
WHERE u.id != b.user_id
AND b. = 0
看,您正在booking.user_id = user.id
和
那么你只是在你的where WHERE user.id != booking.user_id
;
更像是在查看Set A
中Set B
和A intersection B
之间的差异。所以在这种情况下你不会找到任何(即NULL)。