SQL彙總 一條sql實現分條件count

2022-07-17 02:03:10 字數 430 閱讀 4328

--適當借鑑高手經驗,原帖忘記那裡了,很抱歉

當你需要查詢同時滿足乙個字段(兩個不同條件)的資料時,而且資料量又比較大,推薦使用以下方法,可以適當提高效率

示例如下:

select d.channel_id,count(case when d.editor_id<0 then 0 end ) as autocount,count(0) as sumcount

from ad_data as d

where d.create_time between '2013-07-16 00:00:00' and '2013-08-16 23:59:59'

and d.del_status=0

備註:多個字段或者多個條件還沒試,不過感覺問題也不大

當然,多個count一定要指定別名,在這種查詢方式下,沒有值的那個count行,會預設填充0

一條sql 分類彙總

表結構 stuid stuname stucourse stugrade 001 a 語文 88 002 b 語文 89 003 a 數學 98 004 b 數學 100 005 a 英語 87 006 b 英語 86 mysql select groupid,stuid,stuname,cours...

SQL 如何實現一條sql語句插入1000行資料

用 sql的可程式設計性,作為測試資料用是吧 declare i int 申明乙個整形變數i set i 1 設定初始值為1 while i 1000 用 while 迴圈給定乙個迴圈結束條件小於1000 begin insert into tb user values user no cast i...

一條SQL語句研究

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