0
我試圖從表中獲取一組集合,我無法弄清楚如何使用LEFT JOIN從相關表'like_collections'中計算喜歡的數目句法。MySQL計算來自INNER關係的項目數
正如你可以在查詢中看到,每個collection_id我需要得到用戶的,像這樣的收集數量。
我知道我只能使用(SELECT COUNT(*) FROM likes_collections WHERE collection_id=cn.id) as n_likes
,但想知道是否可以使用下面的查詢。
SELECT cn.id,
cn.name,
cn.description,
u.avatar,
u.username,
COUNT(lc.id) as n_likes,
(SELECT COUNT(*) FROM collection_items WHERE collection_id=cn.id) as n_items
FROM collection_names as cn
INNER JOIN users as u ON u.ID=cn.user_id
LEFT JOIN likes_collections as lc ON lc.collection_id=cn.id
WHERE cn.public=1
GROUP BY lc.collection_id
ORDER BY cn.published_date DESC
LIMIT 0, 5
「n_likes」後面缺少逗號。 – Barmar
謝謝,我更新了查詢。 – domoindal