我使用 MYSQL ;
有3个表关系首先作为车手表示例:
bib | series_id | point
202 3 200
219 3 140
202 2 200
219 2 110
10 1 90
第二个例子为系列表:
series_id | series_no | season_id
1 1 2
2 2 1
3 1 1
示例第三个作为季节表:
season_id | year
1 2015
2 2016
如何正确指出GROUP_CONCAT
?我试着这样做
SELECT riders.bib, seasons.year, GROUP_CONCAT(DISTINCT riders.point ORDER BY series.series_no DESC) AS seriPoint
FROM series, riders, seasons
GROUP BY riders.bib
当我使用bib: 202 is 200 and bib: 219 is 140,110
输出时,我正在为DISTINCT
获取输出seriPoint。但是当我没有使用DISTINCT
获取bib: 202 is 200,200,200,200 and bib: 219 is 140,110,140,110
的输出seriPoint时。我想要的是为bib: 202 is 200,200 and bib: 219 is 140,110
输出seriPoint。
ADD:也可以帮助添加过滤器,season_id
当不同的season_id
为不同的行时。
答案 0 :(得分:1)
是的,因为您使用了DISTINCT
,所以输出正确。顺便说一下,你应该改变你的查询以使用正确的JOINS
SELECT riders.bib,
seasons.year,
GROUP_CONCAT(DISTINCT riders.point ORDER BY series.series_no DESC) AS seriPoint
FROM riders
JOIN series ON series.series_id = riders.series_id
JOIN seasons ON series.season_id = seasons.season_id
GROUP BY riders.bib;
(或)您可以先获得分组,然后执行
之类的连接select seasons.year, xx.bib, xx.seriPoint
FROM series
JOIN (
select series_id, bib
group_concat(point) as seriPoint
from riders
group by bib ) xx ON series.series_id = xx.series_id
JOIN seasons ON series.season_id = seasons.season_id
order by xx.seriPoint;