我有3张桌子:
如何让所有用户同时拥有他们拥有的下载次数?
答案 0 :(得分:3)
如何让所有用户同时拥有他们拥有的下载次数?
使用:
SELECT u.name,
COUNT(d.user_id) 'num_downloaded'
FROM USERS u
OIN DOWNLOADS d ON d.user_id = u.user_id
GROUP BY u.name
如果您想查看用户下载特定项目的次数:
SELECT u.name 'user_name',
i.name 'item_name',
COUNT(d.user_id) 'num_downloaded'
FROM USERS u
JOIN DOWNLOADS d ON d.user_id = u.user_id
JOIN ITEMS i ON i.item_id = d.item_id
GROUP BY u.name, i.name
答案 1 :(得分:0)
这应该这样做:
SELECT u.name as name, count(*) as downloads
FROM users u, items i, downloads d
WHERE u.id = i.user_id AND i.user_id = d.user_id;
我必须发明items.user_id
,但它应该可用。