SELECT contest.contest_id
FROM db_user
LEFT JOIN participants ON participants.user_id = db_user.db_user
LEFT JOIN contest ON participants.contest_id = contest.contest_id
WHERE contest.user_id=$user
group by participants.contest_id,participants.user_id";
这将返回所有参与竞赛并且运行良好的用户,但我需要找到它返回的行数。 例如,上面返回5行 我希望结果为
Contest_Id Number of users
1 3
2 4
答案 0 :(得分:0)
您需要使用COUNT
聚合函数以及相应的HAVING
子句。请参阅以下内容:HAVING Clause
答案 1 :(得分:0)
试试这个:
select contest.contest_id ,Count(*) as 'No of Users'
FROM db_user
LEFT JOIN participants ON participants.user_id = db_user.db_user
LEFT JOIN contest ON participants.contest_id = contest.contest_id
WHERE contest.user_id=$user
group by participants.contest_id;
答案 2 :(得分:0)
尝试:
SELECT contest.contest_id,
(select count(*) from db_user
inner JOIN participants ON participants.user_id = db_user.user_id and
participants.contest_id = contest.contest_id)
FROM contest
答案 3 :(得分:0)
您可能希望扩展SELECT
语句以包含COUNT(participants.user_id)
。如果用户两次参加相同的比赛,他将被重复计算。
答案 4 :(得分:0)
您的查询在语法上是错误的,因为您应该始终选择GROUP BY
子句中提到的字段,尽管MySQL允许您指定其他字段,但您应该知道大多数情况下您将获得不可预测的结果。话虽如此,也许这是您正在寻找的查询:
SELECT participants.contest_id, COUNT(DISTINCT participants.user_id) AS 'Number of users'
FROM db_user
LEFT JOIN participants ON participants.user_id = db_user.db_user
LEFT JOIN contest ON participants.contest_id = contest.contest_id
WHERE contest.user_id = $user
GROUP BY participants.contest_id;