MySQL SELECT n records base on GROUP BY

后端 未结 3 935
抹茶落季
抹茶落季 2021-01-19 19:10

Lets say I have SQL records:

Country | Number
USA | 300
USA | 450
USA | 500
USA | 100
UK  | 300
UK  | 400
UK  | 1000

And I am doing something like this

3条回答
  •  -上瘾入骨i
    2021-01-19 19:37

    Lets name your table TName, then they query would be.

    SELECT * FROM (SELECT ROW_NUMBER() OVER (PARTITION BY X.Country) AS RowNo, *
    FROM (SELECT Country, Name FROM TName ORDER BY Country, Number) X ) Y WHERE Y.RowNo <= 2
    

提交回复
热议问题