mysql多表多個資料統計語句

2021-08-17 01:23:31 字數 538 閱讀 8799

1.前一段時間開發做poi開發word,用到資料統計,介於多資料count統計,寫多條sql太low,還影響效能,所以查閱了一些資料總結了一套sql,感謝mysql。

場景需求:需要在表a,b,c的一些條件下,查詢出合格資訊和不合格資訊,以及總數。

sql:select

sum(case when c.checkresult = 『合格』 then 1 else 0 end) as qualified,

sum(case when c.checkresult = 『不合格』 then 1 else 0 end) as disqualification,

count(*) as count from a,b,c where c.year like 『2018』 and …;

完美的統計出合格數、不合格數、總數。

2.sql中ifnull用法:

select ifnull(x,0) from a;

解釋:如果表a中x欄位是null,輸出為0

擴充套件用法:select ifnull(sum(x),0) from a;

資料統計儲存過程規範(mysql)

在乙個需要做大量資料統計分析的專案裡,免不了需要寫儲存過程。如果過程不多不會有問題,但多了以後,維護和監控會產生很多問題。這時候,最好是對儲存過程進行統一規範,對異常進行處理和對運 況做日誌記錄。也使不同的人編寫的過程格式一致,方便維護。1 建立日誌表 create table tb proc lo...

表元資料統計 統計MySQL資料表大小

需要統計表的元資料基本資訊 如統計表的容量大小 行數等,可引數如下語句 select table name,data length 1024 1024 as datam index length 1024 1024 as indexm,data length index length 1024 10...

多表資料統計彙總入新的記錄表中

delimiter drop procedure if exists bookstatistical create procedure bookstatistical begin 宣告變數 declare b day varchar 10 declare b id int 設定游標初始值為0 dec...