我需要一个想要执行此操作的mysql查询
initial data
id | from_id | to_id | route_id | price
1 | 360 | 415 | 204 | 100
2 | 372 | 458 | 204 | 100
3 | 395 | 372 | 204 | 100
4 | 395 | 372 | 204 | 100
5 | 395 | 372 | 205 | 100
6 | 395 | 372 | 204 | 100
7 | 395 | 458 | 210 | 100
8 | 395 | 458 | 210 | 100
result data
id | from_id | to_id | route_id | price
1 | 395 | 372 | 204 | 100 // because his periodicity is 3
2 | 395 | 458 | 210 | 100 // because his periodicity is 2
3 | 395 | 372 | 205 | 100
4 | 360 | 415 | 204 | 100
5 | 372 | 458 | 204 | 100
是这样的
SELECT * FROM myTable GROUP BY from_id,to_id,route_id
但我不知道如何添加COUNT或MAX和ORDER BY 感谢
答案 0 :(得分:1)
SELECT
min( id ) FirstID,
from_id,
to_id,
route_id,
max(price) MaxPrice,
count(*) as Periodicity
from
myTable
GROUP BY
from_id,
to_id,
route_id
您的最终ID表看起来更像是顺序,但与原始数据无关,并且似乎没有任何其他目的。