sql利用在一條語句中count出不同的條件的值

2021-08-03 12:50:16 字數 882 閱讀 6150

過多繁瑣的sql影響**質量,及維護成本,以下為兩種小技巧處理方式,僅供參考,第二種更美觀點

第一種,用case ---when---方法

selectid,

sum(

case

when

type 

in(1,2) 

then

[count

] else

0 end

) as

sum1

,sum

(case

when

type 

in(3) 

then

[count

] else

0 end

) as

sum2

,sum

(case

when

type 

in(4,5) 

then

[count

] else

0 end

) as

sum3

from

表名group

byid

第二種,if 判斷

select   

sum( goods_amount ) as money,   

count( * ) as num,  

count(if(

pay_status=1

,true,null)) as success,   

count(if(

pay_status=2

,true,null)) as fall   

from `tab_order_info`     

where 

user_id

= 11

一條SQL語句研究

現有 select from t where a in 5,3,2,1,8,9,30.假設 a 是主鍵,in裡面的引數是唯一的。現要求輸出的結果集按照 in 提供的引數順序排序。而不是按照a本身的排序規則排序?另 如果不要求使用臨時表或表變數,那麼又有什麼辦法實現。臨時表方案參卡 create ta...

sql語句ding 求一條sql語句

我理解你的問題是每乙個使用者id在乙個部門中所有的許可權,你給的資料好像不詳細,我新增了一些資料,你看看滿足需求不。sql with authority as 2 select 3029 kuid,205 krid,21 kdid from dual union all 3 select 3029 ...

SQL語句中一條常見的行合併問題

sql行合併問題一般寫個自定義函式,由orig word 統計similar word,就可以實現 問題 原來問題 現有兩個表 表一originalword id word 101 about 102 abound 103 beard 104 boast 105 beast 表二similarwor...