[英]MySQL Join Query to Sum a Field in One Table based on Its Corresponding Login ID in another Table
I have a MySQL table comment
with the following fields:我有一个 MySQL 表
comment
,其中包含以下字段:
loginid submissionid points
I have a MySQL table called submission
with the following fields:我有一个名为
submission
的 MySQL 表,其中包含以下字段:
loginid submissionid
For a given submissionid
, the loginid
in the two tables represent different things and therefore do nor correspond.对于给定的
submissionid
,两个表中的loginid
代表不同的事物,因此也不对应。
I would like a join to sum up points
by loginid
.我想通过
loginid
来总结points
。 However, not by the loginid
in comment
, but rather the loginid in submission
.但是,不是
comment
中的loginid
,而是submission
中的登录名。 The connection between the two tables is made via the submissionid
两个表之间的连接是通过
submissionid
I can't get this to work.我不能让它工作。 Below is what I have so far.
以下是我到目前为止所拥有的。 I'm trying to get this desired sum for each loginid pulled from another, third table, which is what the
l.loginid
represents.我正在尝试为从另一个第三个表中提取的每个 loginid 获得所需的总和,这就是
l.loginid
代表的内容。
How can I do this?我怎样才能做到这一点?
LEFT JOIN (
SELECT C2.submissionid, C2.loginid SUM(points) AS total
FROM comment C2
INNER JOIN submission S2
ON S2.submissionid = C2.submissionid
GROUP BY C2.submissionid
) cscs ON cscs.loginid = l.loginid
SELECT S2.loginid, SUM(points) AS total
FROM submission S2
INNER JOIN comment C2
ON S2.submissionid = C2.submissionid
GROUP BY S2.loginid
This will give you a points sum for each loginid in the submission table.这将为您提供提交表中每个登录 ID 的积分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.