我正在处理一个相当复杂的SQL
声明。我在这里:
SELECT c.category
FROM master_cat as c
LEFT JOIN
(
SELECT cat_id, user_id COUNT(cat_id) favoriteCat
FROM ratings
GROUP BY user_id
) a ON a.cat_id= c.cat_id
LEFT JOIN users AS u
ON u.user_id AND a.user_id
WHERE u.username = '{$user}' LIMIT 1
此声明不完整。我在这里错过了一张中间牌桌。 cat_id
实际上并不在ratings
。但items_id
来自名为items
的表格,cat_id
也位于该表格中。
所以我想做的是:
SELECT rating FROM ??? GROUP BY cat_id where u.user=$user
我能想到的唯一一件事就是在items
内与favoriteCat
进行另一次LEFT加入,但我不确定是否允许这样做。
答案 0 :(得分:0)
我在思考这个问题,这是我的最终解决方案:
SELECT c.category, count(r.rating) AS totalCount
FROM ratings as r
LEFT JOIN items AS i
ON i.items_id = r.item_id
LEFT JOIN users AS u
ON u.user_id = r.user_id
LEFT JOIN master_cat AS c
ON c.cat_id = i.cat_id
WHERE r.user_id = '{$user_id}'
GROUP BY c.category
ORDER BY totalCount DESC