MySQL Next group takes the association table to specify the prompt method, similar to the cross apply in MSSQL

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.