我正在使用一些MySQL表。他们的结构如下所列。
“登录”:
loginid username password email actcode disabled activated created points website location age gender
每个用户都有一个loginid
“提交”:
submissionid loginid title slug url displayurl datesubmitted
在上面的提交表中,“loginid”是提交提交的用户的loginid。
“注释”:
commentid loginid submissionid comment datecommented
在上面的评论表中,“loginid”是发表评论的用户的loginid。 “submissionid”是提交评论的提交。
下面的查询按照(登录ID存在的天数)+(loginid提交的总数)* 10 +(loginid的注释)对前25个登录名进行排名。
我想再增加一个因素:(对loginid提交的提交者的总评论数)* 10.
需要以下内容:
我该怎么做?
提前致谢,
约翰
$sqlStr2 = "SELECT
l.loginid,
l.username,
l.created,
DATEDIFF(NOW(), l.created) + COALESCE(s.total, 0) * 10 + COALESCE(c.total, 0) AS totalScore2
FROM login l
LEFT JOIN (
SELECT loginid, COUNT(1) AS total
FROM submission
GROUP BY loginid
) s ON l.loginid = s.loginid
LEFT JOIN (
SELECT loginid, COUNT(1) AS total
FROM comment
GROUP BY loginid
) c ON l.loginid = c.loginid
GROUP BY l.loginid
ORDER BY totalScore2 DESC
LIMIT 25";
答案 0 :(得分:0)
将以下内容作为附加联接介绍给您的查询,然后将总计* 10包含在您的totalscore2计算中。
LEFT JOIN (
SELECT S2.loginid, COUNT(1) AS total
FROM submission S2
INNER JOIN comment C2
ON C2.submissionid = S2.submissionid
GROUP BY S2.loginid
) scs ON scs.loginid = l.loginid