MySQL union order by sorting issues separately

Source: Internet
Author: User

Two different ways:

The first type:

Sort them first, and then merge the results with the temporary table nesting, noting that the order must be preceded by a limit, otherwise the order by does not work

SELECT * FROM (SELECT * to T1 WHERE ID in (1,3,6) ORDER by Utime DESC limit 5) as a
UNION All
SELECT * FROM (SELECT * to T1 WHERE ID in (2,4,5) ORDER by Utime DESC limit 5) as B
;
The second type:

After all results are detected by adding a temporary column px, then sort by px,utime

SELECT * FROM (
Select 0 as Px,id,name,utime from (SELECT * from T1 WHERE ID in (1,3,6)) as a
UNION All
Select 1 as Px,id,name,utime from (SELECT * from T1 WHERE ID in (2,4,5)) as B
) as C ORDER by Px,utime DESC


MySQL union order by sorting issues separately

Related Article

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.