在 UNION mysql 中仅在一个查询中排序
order by in only one query in UNION mysql
我有一个table
Table 用户
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')
我总是在 ASC
和 DESC
这两种情况下得到这个
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
请帮助我。您的帮助将不胜感激。
您可以使用 case when
作为 order by
select * from User
order by
case when status <> 'Approved' then 0 else 1 end ,status;
尝试将 union 更改为 union all,它至少在 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
希望对您有所帮助。 :)
感谢您的回复。我得到了解决方案
(select * from
(select * from user where status <> 'Approved' order by status asc) a)
union
(select * from t1 where status = 'Approved' )
我有一个table
Table 用户
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')
我总是在 ASC
和 DESC
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
请帮助我。您的帮助将不胜感激。
您可以使用 case when
作为 order by
select * from User
order by
case when status <> 'Approved' then 0 else 1 end ,status;
尝试将 union 更改为 union all,它至少在 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
希望对您有所帮助。 :)
感谢您的回复。我得到了解决方案
(select * from
(select * from user where status <> 'Approved' order by status asc) a)
union
(select * from t1 where status = 'Approved' )