在MySQL服务器中工作,我希望运行多个用户ID的查询来返回统计信息。
select
Firstname,
Lastname,
email,
count(distinct startTime) as "total Rounds",
MAX(starttime) as "Last Round"
from users.users
join rounds.rounds on rounds.rounds.userId = users.users.userId
where users.userid and rounds.userId ="ENTER A USER ID HERE"
有关为多个用户获取结果集的任何建议吗?
答案 0 :(得分:1)
使用group by
:
select Firstname, Lastname, email, count(distinct startTime) as "total Rounds",
MAX(starttime) as "Last Round"
from users.users u join
rounds.rounds r
on r.userId = u.userId
group by Firstname, Lastname, email;
另外,学习使用表别名。它们使查询更容易编写和阅读。
答案 1 :(得分:1)
您可以使用COUNT()函数,但只需使用GROUP BY来获取每个用户的计数,如下所示:
SELECT firstName, lastName, email, COUNT(distinct startTime) AS totalRounds, MAX(startTime) AS lastRound
FROM users u JOIN rounds r ON r.userId = u.userId
GROUP BY u.userId;
这将按个别用户对所有行进行分组,计算该用户的唯一开始时间以及最新的开始时间,但我确定您现在已了解该概念。