Last active
September 28, 2021 09:32
-
-
Save lysender/545dd2d2dfa5fc7aad56feb77a3e00a7 to your computer and use it in GitHub Desktop.
MySQL Select top n for each group
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set @num := 0, @type := ''; | |
select type, variety, price | |
from ( | |
select type, variety, price, | |
@num := if(@type = type, @num + 1, 1) as row_number, | |
@type := type as dummy | |
from fruits | |
order by type, price | |
) as x where x.row_number <= 2; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment