如何 group_concat 正确使用 distinct - MYSQL

how to group_concat using distinct correctly - MYSQL

我有 3 个 table 关系使用 MYSQL;

首先以车手为例table:

bib   |   series_id    | point
 202         3             200
 219         3             140
 202         2             200
 219         2             110
 10          1             90

第二个示例作为系列 table:

series_id   |   series_no  |   season_id
     1            1                2
     2            2                1
     3            1                1

示例第三季 table:

 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

当我像那样使用 DISTINCT 输出时,我得到 bib: 202 is 200 and bib: 219 is 140,110 的输出 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 时它是不同的行。

是的,你得到了正确的输出,因为你已经使用了 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;

(OR) 你可以先获取分组再执行join like

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;