Home >Database >Mysql Tutorial >mysql-求助一条SQL语句及原理,具体请看描述!

mysql-求助一条SQL语句及原理,具体请看描述!

WBOY
WBOYOriginal
2016-06-06 09:33:281290browse

mysqlsql

图片说明
如图所示订单表 pid 为null的是主订单
需求是 按主订单ddate倒序排列,子订单排在主订单后面
最终结果如图:
图片说明
我写的SQL语句可以实现

<code> SELECT DISTINCT id from (    SELECT b.id,  a.ddate FROM `tttest` a , `tttest` b    where a.id = b.pid    union    SELECT c.id,c.ddate from tttest c ) t ORDER BY t.ddate desc ,id</code>

但是太复杂 求一条简介的SQL

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn