MySQL - 选择旅行次数最多但未重复的项目

MySQL - Selecting the items that have the highest count of trips but not repeated

我正在尝试创建一个系统,它可以找到行程最多的停靠点列表 (stop_code)。

喜欢:

SELECT count(`trip_id`) as `count` FROM `stop_times` 
WHERE `time` BETWEEN '13:00:00' AND '13:05:00' 
GROUP BY `stop_code` 
ORDER BY `count` DESC

但是因为,我正在拉取结果

stop code: 3000 count: 9    
stop code: 3011 count: 7    
stop code: 3030 count: 3

在这些计数中可以说,它包括:

stop code: 3000 list: trip1,trip2,trip3,trip4,trip5,trip6,trip7,trip8,trip9
stop code: 3011 list: trip3,trip4,trip5,trip6,trip7,trip10,trip11
stop code: 3030 list: trip2,trip3,trip4,trip12

我如何调整该查询以使其成为:

stop code: 3000 list: trip1,trip2,trip3,trip4,trip5,trip6,trip7,trip8,trip9
stop code: 3011 list: trip10,trip11
stop code: 3030 list: trip12

作为一组独特的 trip_id

谢谢

在进行计数之前,使用为每个 trip_id 获取最低 stop_code 的子查询。

SELECT stop_code, COUNT(*) AS count
FROM (SELECT trip_id, MIN(stop_code) AS stop_code
      FROM stop_times
      WHERE `time` BETWEEN '13:00:00' AND '13:05:00' 
      GROUP BY trip_id) AS x
GROUP BY stop_code
ORDER BY count DESC