如果有一张表格显示一个组织成员所获得的奖励,我试图列出在给定日期之后获得三个或更多奖励的所有成员,并列出这些成员所获得的所有奖励。我有一个能够正确列出成员的查询,但每个成员只显示一行,我试图查看他们的所有奖项。
每个奖项(“奖励”)的表格中都有一行,奖励类型和获奖日期(“Awarddate”)。
这就是我所拥有的:
SELECT Membername, Award, Awarddate,COUNT(Membername)
FROM awards
WHERE Awarddate > '2011-06-30'
GROUP BY Membername HAVING COUNT(Membername) > 2
建议?
答案 0 :(得分:1)
您可以使用GROUP_CONCAT()
聚合函数:
SELECT Membername
, COUNT(*) AS NumberOfAwards
, GROUP_CONCAT(Award ORDER BY Awarddate) AS Awards
, GROUP_CONCAT(Awarddate ORDER BY Awarddate) AS AwardDates
FROM awards
WHERE Awarddate > '2011-06-30'
GROUP BY Membername
HAVING COUNT(*) > 2
答案 1 :(得分:1)
SELECT a.Membername, a.Award, a.Awarddate, Awardcount.Awards
FROM awards a
JOIN
( SELECT Membername, COUNT(*) AS Awards
FROM awards
WHERE Awarddate > '2011-06-30'
GROUP BY Membername
HAVING Awards > 2 ) Awardcount
ON a.Membername = Awardcount.Membername
修改:仅列出在给定日期之后获得的奖励,按照ypercube的建议为此查询添加一行:
WHERE a.Awarddate > '2011-06-30'