只在UNION mysql中的一个查询中排序

时间:2015-09-15 15:03:39

标签: mysql union

我有一张桌子

用户

 Id      Name     status        
 1       Jhon     Approved        
 2       Endy     Decline
 3       Ally     In Review

我必须显示所有用户的列表,并仅对那些未经批准的行进行排序。

我的代码在

下面
(select name from user where status<>'Approved' order by status asc)
UNION  
(select name from user where status='Approved')

我总是在ASCDESC

两种情况下得到此信息
Id      Name     status             
 3      Ally     In Review
 2      Endy     Decline
 1      Jhon     Approved   

name的订单也应该是相同的结果。

(select name from user where status<>'Approved' order by name asc)
UNION  
(select name from user where status='Approved')

 Id      Name     status             
 3       Ally     In Review
 2       Endy     Decline
 1       Jhon     Approved   

请帮帮我。非常感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

您可以将case when用于order by

select * from User 
order by  
case when status <> 'Approved' then 0 else 1 end ,status;

答案 1 :(得分:0)

尝试使用union all更改union,它至少在oracle

中起作用
select * from 
(select * from t1 where status <> 'Approved' order by status asc)
union all
select * from 
(select * from t1 where status = 'Approved' )

输出:

ID|NAME|STATUS
2|Endy|Declined
3|Ally|In Review
1|John|Approved

希望它可以帮到你。 :)

答案 2 :(得分:0)

感谢您的回复。我得到了解决方案

(select * from 
(select * from user where status <> 'Approved' order by status asc) a)
union 
(select * from t1 where status = 'Approved' )