codes
包含应分配给特定users
的代码codes_users
保存连接信息。
SELECT users.*,
codes.code
FROM users
LEFT JOIN (codes
INNER JOIN codes_users
ON codes.id = codes_users.code_id)
ON users.id = codes_users.user_id
我已经设法在这个小提琴中显示代码:http://sqlfiddle.com/#!2/acd0cb/1
但是当我想计算下载时,我只得到一个结果:
SELECT users.*,
codes.code,
Sum(users.downloads) AS _downloadscount
FROM users
LEFT JOIN (codes
INNER JOIN codes_users
ON codes.id = codes_users.code_id)
ON users.id = codes_users.user_id
http://sqlfiddle.com/#!2/acd0cb/2
我也想拥有每个用户的下载次数
答案 0 :(得分:2)
您的查询最后需要一个小组,告诉DB您希望计算每个用户
SELECT users.*,
codes.code,
Sum(users.downloads) AS _downloadscount
FROM users
LEFT JOIN (codes
INNER JOIN codes_users
ON codes.id = codes_users.code_id)
ON users.id = codes_users.user_id
GROUP BY users.id;
答案 1 :(得分:1)
SELECT users.*, codes.code, SUM(users.downloads) AS _downloadscount FROM users
LEFT JOIN
(codes INNER JOIN codes_users ON codes.ID = codes_users.code_id)
ON users.ID = codes_users.user_id
group by users.id