乙個SQL語句的三種寫法

2021-08-29 21:32:58 字數 667 閱讀 2110

表table中有一欄位type,要用一sql語句查詢type為1的記錄數加上type為2的記錄數的一半。

寫法1:

select count(case when t.tasktype=1 then 1 end) 

+ count(case when t.tasktype=2 then 1 end)/2

from table t;

寫法2:

select sum(counter) 

from ((select count(*) counter from table t where t.type=1)

union (select count(*)/2 from table t where t.type=2));

寫法3:

select (a+b/2) 

from (select count(*) as a from table t where t.tasktype=1),

(select count(*) as b from table t where t.tasktype=2);

當然肯定不止這三種辦法,這三種只是作為參考,拓寬一下思路,很多時候辦法不止一種,別一條路走到死。

insert into 語句的三種寫法

方式1 insert into t1 field1,field2 value v001,v002 明確只插入一條value 方式2 insert into t1 field1,field2 values v101,v102 v201,v202 v301,v302 v401,v402 在插入批量資料時...

for迴圈的三種寫法

1 遍歷迴圈 for 迴圈變數型別 迴圈變數名稱 迴圈條件 更新語句 迴圈體 string arr for int i 0 i arr.length i 列印台abc d2 迭代器迴圈 string arr listlist arrays.aslist arr for iteratoriterato...

setInterval 的三種寫法

前言 setinterval fun time 有兩個引數 fun 為要執行的函式 time為多久執行一次函式,單位是毫秒 我們做乙個簡單的例子,就是每隔5s彈出乙個 hello 的對話方塊。先看第一種寫法,把方法體抽離出來,以字串的形式呼叫函式名,這種寫法呼叫函式名是不能傳參的 第二種寫法是把要執...