Home > Database > Mysql Tutorial > body text

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

WBOY
Release: 2016-06-06 09:33:28
Original
1240 people have browsed it

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>
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template