mysql中怎么让union all不打乱顺序
sql1:select * from t where a is not null order by a; sql2:select * from t where a is null order by b; sql1 union all sql2; sql1和sql2分别查询的时候是正确的,union all 后顺序就打乱了,怎么才能让他保持原来的顺序呢?即:先显示sql1的结果集,再显示sql2的结果集
sql1:select * from t where a is not null order by a; sql2:select * from t where a is null order by b; sql1 union all sql2; sql1和sql2分别查询的时候是正确的,union all 后顺序就打乱了,怎么才能让他保持原来的顺序呢?即:先显示sql1的结果集,再显示sql2的结果集
答案:在前面在加一列,然后把union all 的结果放到子查询中,用来排序
比如:
select * from
(select 0+a ord,t.* from where a is not null
union all
select 1+b ord,t.* from where a is not null
) ta
order by ord
上一个:mysql查询问题
下一个:mysql中修改表字段