簡體   English   中英

sql如何按聯合的子查詢排序而不添加列?

[英]sql how to sort by a union's subquery without adding a column?

因此,大多數情況下我都有所需的查詢,但是我注意到我仍然從第一個選擇項和第二個選擇項中獲得重復項。 我以為使用UNION而不是UNION ALL會刪除重復項,但是由於它們具有不同的序列號,因此不會刪除它們。 如何在不添加不必要的seq列的情況下通過select語句對結果進行排序?

select 1 as seq, t.* from template t 
WHERE status = 'ACTIVE' and t.title ~* '.*동\s*아\s*리\s*로\s*고\s*.*' 
UNION 
select 2 as seq, t.* from template t 
WHERE status = 'ACTIVE' and t.title ~* any(array['.*동\s*아\s*리\s*로\s*고\s*.*']) 
UNION select 3 as seq, t.* from template t WHERE status = 'ACTIVE' 
order by seq asc

您可以通過以下order by使用order by來執行此操作:

select t.*
from template t 
where status = 'ACTIVE' 
order by (t.title ~* '.*동\s*아\s*리\s*로\s*고\s*.*') desc,
         (t.title ~* any(array['.*동\s*아\s*리\s*로\s*고\s*.*']) desc;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM