MySQL JOINED table 查询应该 return 零但不 return 那些行
MySQL JOINED table query should return zero but instead does not return those rows
SELECT sg.date, sg.groupName, sg.highlights,
user.display_name as displayName,
COUNT(uc.groupName) as cmtcnt
FROM `saved_groups` as sg
LEFT JOIN `user` on user.email = sg.userName
LEFT JOIN `user_comments` as uc on sg.groupName = uc.groupName
WHERE uc.deleted=0
GROUP BY sg.groupName
我有两个 table,saved_groups
和 user_comments
,查询的第二行应该(确实有效)return 一个额外的列与每个组关联的评论数。
然而,当一个群组有零条与之关联的评论时,该群组就不会被 return 编辑。只有 return 行是那些有 > 0
条评论的行。期望的行为是对所有 groupNames 进行 returned,并为在 uc table.
中具有零个关联评论的那些行指定 0
我该如何修正这个查询?
我试过:IF(uc.deleted=1, 0, COUNT(uc.groupName)) as cmtcnt
-- 但这没什么区别,returned 得到的结果相同。
此时,我不确定接下来要尝试什么。
建议?
更新:
试过这个:
SELECT sg.date, sg.groupName, sg.highlights,
user.display_name as displayName,
COUNT(uc.groupName) as cmtcnt
FROM geom.saved_groups as sg
JOIN geom.user on user.email = sg.userName
JOIN geom.user_comments as uc on sg.groupName = uc.groupName
WHERE isnull(uc.deleted,0) in (0,1)
GROUP BY sg.groupName
得到:
#1582 - Incorrect parameter count in the call to native function 'isnull'
检查是否 uc.deleted=0 并删除 where 条件。 the and 处理左连接的非空条件
SELECT sg.date, sg.groupName, sg.highlights,
user.display_name as displayName,
COUNT(uc.groupName) as cmtcnt
FROM dbo.saved_groups as sg
JOIN dbo.user on user.email = sg.userName
LEFT JOIN dbo.user_comments as uc on sg.groupName = uc.groupName
AND uc.deleted = 0
GROUP BY sg.groupName
您的 where 条件正在过滤记录并仅将那些在 uc
中有数据的记录添加到 return,因此左连接没有任何好处。你可以做
(uc.deleted=0 or uc.deleted is null)
要么
将条件 uc.deleted 移动到 on 条件 with join
您想要所有记录吗?然后删除 WHERE
子句。您想要 0
用于没有计数的记录?使用 COALESCE
。像这样:
SELECT sg.date
, sg.groupName
, sg.highlights
, user.display_name as displayName
, COALESCE(COUNT(uc.groupName), 0) as cmtcnt
FROM `saved_groups` as sg
LEFT JOIN `user` on user.email = sg.userName
LEFT JOIN `user_comments` as uc on sg.groupName = uc.groupName
AND uc.deleted = 0 -- to get only comments that have not been deleted, and not the deleted ones
GROUP BY sg.groupName
SELECT sg.date, sg.groupName, sg.highlights,
user.display_name as displayName,
COUNT(uc.groupName) as cmtcnt
FROM `saved_groups` as sg
LEFT JOIN `user` on user.email = sg.userName
LEFT JOIN `user_comments` as uc on sg.groupName = uc.groupName
WHERE uc.deleted=0
GROUP BY sg.groupName
我有两个 table,saved_groups
和 user_comments
,查询的第二行应该(确实有效)return 一个额外的列与每个组关联的评论数。
然而,当一个群组有零条与之关联的评论时,该群组就不会被 return 编辑。只有 return 行是那些有 > 0
条评论的行。期望的行为是对所有 groupNames 进行 returned,并为在 uc table.
我该如何修正这个查询?
我试过:IF(uc.deleted=1, 0, COUNT(uc.groupName)) as cmtcnt
-- 但这没什么区别,returned 得到的结果相同。
此时,我不确定接下来要尝试什么。
建议?
更新:
试过这个:
SELECT sg.date, sg.groupName, sg.highlights,
user.display_name as displayName,
COUNT(uc.groupName) as cmtcnt
FROM geom.saved_groups as sg
JOIN geom.user on user.email = sg.userName
JOIN geom.user_comments as uc on sg.groupName = uc.groupName
WHERE isnull(uc.deleted,0) in (0,1)
GROUP BY sg.groupName
得到:
#1582 - Incorrect parameter count in the call to native function 'isnull'
检查是否 uc.deleted=0 并删除 where 条件。 the and 处理左连接的非空条件
SELECT sg.date, sg.groupName, sg.highlights,
user.display_name as displayName,
COUNT(uc.groupName) as cmtcnt
FROM dbo.saved_groups as sg
JOIN dbo.user on user.email = sg.userName
LEFT JOIN dbo.user_comments as uc on sg.groupName = uc.groupName
AND uc.deleted = 0
GROUP BY sg.groupName
您的 where 条件正在过滤记录并仅将那些在 uc
中有数据的记录添加到 return,因此左连接没有任何好处。你可以做
(uc.deleted=0 or uc.deleted is null)
要么
将条件 uc.deleted 移动到 on 条件 with join
您想要所有记录吗?然后删除 WHERE
子句。您想要 0
用于没有计数的记录?使用 COALESCE
。像这样:
SELECT sg.date
, sg.groupName
, sg.highlights
, user.display_name as displayName
, COALESCE(COUNT(uc.groupName), 0) as cmtcnt
FROM `saved_groups` as sg
LEFT JOIN `user` on user.email = sg.userName
LEFT JOIN `user_comments` as uc on sg.groupName = uc.groupName
AND uc.deleted = 0 -- to get only comments that have not been deleted, and not the deleted ones
GROUP BY sg.groupName