現在有檢索條件A,B,C
想要的資料和順序是(因為只要20條,所以要排序)
同時滿足A,B,C
+
同時滿足A,B
+
只滿足A
我現在能想到的是用union
select...where A and B and C
union
select...where A and B
union
select...where A
但感覺很low,有挺多重復資料的,用union應該效率很慢
有什么思路嗎?
不然只能代碼里分批次檢索了
uj5u.com熱心網友回復:
不如把資料查出來,程式進行排序,比較快點uj5u.com熱心網友回復:
你是想要這樣?select...where (A and B and C) or (A and B) or (A) ?
其實你的檢索條件里A檢索出來的資料已經包含了(A and B and C)和(A and B) ,除非條件不一樣 A ,(B and C),(D and E and F)。
uj5u.com熱心網友回復:
查詢A條件, 然后根據 三個條件,給出flag 排序取前20條uj5u.com熱心網友回復:
建議放在存盤程序中來取這些資料,如果用單一陳述句實作肯定是寫出來,就是效率會差一些(資料量大了差的會很多)uj5u.com熱心網友回復:
但寫存盤程序我想到也就是
select...where A and B and C
if result1.count < 20
select...where A and B and !C
if result1.count + result2.count < 20
select...where A and !B and !C
檢索3次
這樣效率算好的嘛?
uj5u.com熱心網友回復:
select from t where Aminus
select from t where A AND C AND !B
uj5u.com熱心網友回復:
邏輯沒有問題,效率也是可以的。
-- PS: 單一陳述句,試試這個。
select *
from t
where A
order by case when C then 10 else 0 end + case when B the 1 else 0 end
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/21198.html
標籤:基礎和管理
上一篇:關于SQLdeveloper
