Mysql-ask for an SQL statement and its principles. for details, refer to the description!

Source: Internet
Author: User
! If the pid of the order table is null, the primary order needs to be sorted in descending order of the primary order ddate, and the suborder is placed after the primary order! [Image description ((img.ask.csdn.netupload201603231458703376_47054.jpg) the SQL statement I wrote can implement & quot; SELECTDISTINCTidfrom (S... mysqlsql


If the order table pid is null, the primary order is used.
The requirement is to sort the orders in descending order by the primary order ddate. the sub-orders are placed after the primary order.
Final result

The SQL statement I wrote can be implemented.

Select distinct id from (SELECT B. id,. ddate FROM 'tttest'a, 'tttest' B where. id = B. pid union SELECT c. id, c. ddate from tttest c) t order by t. ddate desc, id

However, it is too complicated to find a brief SQL statement.

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.