我有一个多态关系表"喜欢"我在其中存储喜欢的表"列表","文章","评论"。
表的结构如下:
我正在尝试选择所有列表中的所有列表:
SELECT lists.*, COUNT(DISTINCT likes.id) AS likes FROM lists
LEFT OUTER JOIN likes ON (lists.id = likes.content_id)
WHERE likes.content_type = 'list'
GROUP BY lists.id
但是,它只返回列表中的表格,其中有喜欢的表格中的喜欢。
感谢您的帮助!
答案 0 :(得分:2)
如果您需要左连接,则必须包含on子句中的where条件,否则我将其用作内连接
SELECT lists.*, COUNT(DISTINCT likes.id) AS likes
FROM lists
LEFT OUTER JOINlikes ON
(lists.id = likes.content_id
AND likes.content_type = 'list')
GROUP BY lists.id