對於所有的需求,當你不知道怎麼處理的時候,你就先用最簡單的方法,或者說的明白一點,用最原始的方法,先實現業務需求再說。 一、對提現隊列數據表“ims_checkout_task”進行彙總統計,按月彙總統計每個月的提現總額,提現總次數。 1、SQL操作如下: 2、資料庫返回如下: 3、關鍵詞:case ...
對於所有的需求,當你不知道怎麼處理的時候,你就先用最簡單的方法,或者說的明白一點,用最原始的方法,先實現業務需求再說。
一、對提現隊列數據表“ims_checkout_task”進行彙總統計,按月彙總統計每個月的提現總額,提現總次數。
1、SQL操作如下:
SELECT id ,SUM(case when FROM_UNIXTIME(addTime,'%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 11 MONTH),'%Y-%m') AND `status` = 1 then money else 0 end) as '0' ,SUM(case when FROM_UNIXTIME(addTime,'%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 10 MONTH),'%Y-%m') AND `status` = 1 then money else 0 end) as '1' ,SUM(case when FROM_UNIXTIME(addTime,'%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 9 MONTH),'%Y-%m') AND `status` = 1 then money else 0 end) as '2' ,SUM(case when FROM_UNIXTIME(addTime,'%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 8 MONTH),'%Y-%m') AND `status` = 1 then money else 0 end) as '3' ,SUM(case when FROM_UNIXTIME(addTime,'%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 7 MONTH),'%Y-%m') AND `status` = 1 then money else 0 end) as '4' ,SUM(case when FROM_UNIXTIME(addTime,'%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 6 MONTH),'%Y-%m') AND `status` = 1 then money else 0 end) as '5' ,SUM(case when FROM_UNIXTIME(addTime,'%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 5 MONTH),'%Y-%m') AND `status` = 1 then money else 0 end) as '6' ,SUM(case when FROM_UNIXTIME(addTime,'%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 4 MONTH),'%Y-%m') AND `status` = 1 then money else 0 end) as '7' ,SUM(case when FROM_UNIXTIME(addTime,'%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 3 MONTH),'%Y-%m') AND `status` = 1 then money else 0 end) as '8' ,SUM(case when FROM_UNIXTIME(addTime,'%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 2 MONTH),'%Y-%m') AND `status` = 1 then money else 0 end) as '9' ,SUM(case when FROM_UNIXTIME(addTime,'%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m') AND `status` = 1 then money else 0 end) as '10' ,SUM(case when FROM_UNIXTIME(addTime,'%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 0 MONTH),'%Y-%m') AND `status` = 1 then money else 0 end) as '11' FROM ims_checkout_task
2、資料庫返回如下:
3、關鍵詞:case when
//流程式控制制語句case語法,例如,如果sex欄位值為1,則輸出男;如果sex值為2,則輸出女;否則輸出其他 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END
//所以上面的SQL為,如果條件成立,則輸出提現金額money欄位,否則輸出0.
時間處理
//對時間戳格式化成 2018-10 FROM_UNIXTIME(addTime,'%Y-%m') //SQL獲取當前時間格式 2019-08 ,根據expr值不同,依次獲取前一個月1,前兩個月2 ··· DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 0 MONTH),'%Y-%m') // 函數用於以不同的格式顯示日期/時間數據。 DATE_FORMAT(date,format) //函數從日期減去指定的時間間隔。 DATE_SUB(date,INTERVAL expr type) //函數返回當前的日期。 CURDATE()
例如
SELECT NOW(),CURDATE(),CURTIME()
例如
SELECT DAYOFWEEK(NOW()),WEEKDAY(now()),DATE_FORMAT(NOW(),"%w"),NOW()
二、對積分訂單數據表按周彙總統計訂單量,比如今天是周二,返回周一到周二的每天單量彙總數據,依次類推
1、SQL操作如下:
SELECT id ,SUM(case when DATE_FORMAT(FROM_UNIXTIME(pay_time,'%Y-%m-%d'),'%w') = 1 AND WEEK(date_add(curdate(),interval 6 day),2) = WEEK(date_add(FROM_UNIXTIME(pay_time,'%Y-%m-%d'),interval 6 day),2) AND `pay_status` = 1 then 1 else 0 end) as '0' ,SUM(case when DATE_FORMAT(FROM_UNIXTIME(pay_time,'%Y-%m-%d'),'%w') = 2 AND WEEK(date_add(curdate(),interval 6 day),2) = WEEK(date_add(FROM_UNIXTIME(pay_time,'%Y-%m-%d'),interval 6 day),2) AND `pay_status` = 1 then 1 else 0 end) as '1' ,SUM(case when DATE_FORMAT(FROM_UNIXTIME(pay_time,'%Y-%m-%d'),'%w') = 3 AND WEEK(date_add(curdate(),interval 6 day),2) = WEEK(date_add(FROM_UNIXTIME(pay_time,'%Y-%m-%d'),interval 6 day),2) AND `pay_status` = 1 then 1 else 0 end) as '2' ,SUM(case when DATE_FORMAT(FROM_UNIXTIME(pay_time,'%Y-%m-%d'),'%w') = 4 AND WEEK(date_add(curdate(),interval 6 day),2) = WEEK(date_add(FROM_UNIXTIME(pay_time,'%Y-%m-%d'),interval 6 day),2) AND `pay_status` = 1 then 1 else 0 end) as '3' ,SUM(case when DATE_FORMAT(FROM_UNIXTIME(pay_time,'%Y-%m-%d'),'%w') = 5 AND WEEK(date_add(curdate(),interval 6 day),2) = WEEK(date_add(FROM_UNIXTIME(pay_time,'%Y-%m-%d'),interval 6 day),2) AND `pay_status` = 1 then 1 else 0 end) as '4' ,SUM(case when DATE_FORMAT(FROM_UNIXTIME(pay_time,'%Y-%m-%d'),'%w') = 6 AND WEEK(date_add(curdate(),interval 6 day),2) = WEEK(date_add(FROM_UNIXTIME(pay_time,'%Y-%m-%d'),interval 6 day),2) AND `pay_status` = 1 then 1 else 0 end) as '5' ,SUM(case when DATE_FORMAT(FROM_UNIXTIME(pay_time,'%Y-%m-%d'),'%w') = 0 AND WEEK(date_add(curdate(),interval 6 day),2) = WEEK(date_add(FROM_UNIXTIME(pay_time,'%Y-%m-%d'),interval 6 day),2) AND `pay_status` = 1 then 1 else 0 end) as '6' FROM ims_integral_order
2、資料庫返回如下:
3、關鍵詞
//格式化時間戳,返回星期數,註意周日返回值為0 DATE_FORMAT(FROM_UNIXTIME(pay_time,'%Y-%m-%d'),'%w') //返回當前時間為一年中第幾周 WEEK(DATE_ADD(CURDATE(),interval 6 day),2) //獲取指定日期是一年中的第幾周 WEEK(date,mode) //函數嚮日期添加指定的時間間隔。 DATE_ADD(date,INTERVAL expr type)
//所以上面的查詢條件為星期和第幾周同時滿足