Go to: Https://stackoverflow.com/questions/12113699/get-top-n-records-for-each-group-of-grouped-results
By grouping the sort and ordinal to get the number of bar information, you can use to index, no test performance, do not know and MSSQL cross apply performance difference is how much, just can achieve the corresponding effect.
1#MySQL5.7. A2#pleaseDropObjects've created at the end of the script3 #or Check for their existance before creating4 #'\\'Is a delimiter5 6 7 CREATE TABLE Test8 (' person ' varchar (5), ' Group ' int, ' age ' int)9 ;Ten One INSERT into Test A (' person ', ' Group ', ' age ') - VALUES - ('Bob', 1, +), the ('Jill', 1,), - ('Shawn', 1,), - ('Jake', 2,), - ('Paul', 2, +), + ('Laura', 2, (+) - ; + A Select person, ' group ', age at from - ( - Select person, ' group ', age, - (@num: =if (@group = ' group ', @num +1, if (@group: = ' group ', 1, 1)) Row_number - From Test T - Cross JOIN (select @num: =0, @group: =null) C in ORDER BY ' Group ', age desc, person - ) as X to where X.row_number <= 2; + - the drop table Test
MySQL Next group takes the association table to specify the prompt method, similar to the cross apply in MSSQL