真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

SQL對(duì)數(shù)據(jù)進(jìn)行按月統(tǒng)計(jì)或?qū)?shù)據(jù)進(jìn)行按星期統(tǒng)計(jì)的實(shí)例代碼

對(duì)于所有的需求,當(dāng)你不知道怎么處理的時(shí)候,你就先用最簡(jiǎn)單的方法,或者說的明白一點(diǎn),用最原始的方法,先實(shí)現(xiàn)業(yè)務(wù)需求再說。

成都創(chuàng)新互聯(lián)公司主要從事成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)資源,十年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18980820575

一、對(duì)提現(xiàn)隊(duì)列數(shù)據(jù)表“ims_checkout_task”進(jìn)行匯總統(tǒng)計(jì),按月匯總統(tǒng)計(jì)每個(gè)月的提現(xiàn)總額,提現(xiàn)總次數(shù)。

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、數(shù)據(jù)庫(kù)返回如下:

SQL對(duì)數(shù)據(jù)進(jìn)行按月統(tǒng)計(jì)或?qū)?shù)據(jù)進(jìn)行按星期統(tǒng)計(jì)的實(shí)例代碼

   3、關(guān)鍵詞:case when

//流程控制語(yǔ)句case語(yǔ)法,例如,如果sex字段值為1,則輸出男;如果sex值為2,則輸出女;否則輸出其他
CASE sex
WHEN '1' THEN '男'
WHEN '2' THEN '女'
ELSE '其他' END
//所以上面的SQL為,如果條件成立,則輸出提現(xiàn)金額money字段,否則輸出0.

時(shí)間處理

//對(duì)時(shí)間戳格式化成 2018-10
FROM_UNIXTIME(addTime,'%Y-%m')
//SQL獲取當(dāng)前時(shí)間格式 2019-08 ,根據(jù)expr值不同,依次獲取前一個(gè)月1,前兩個(gè)月2 ···
DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 0 MONTH),'%Y-%m')
// 函數(shù)用于以不同的格式顯示日期/時(shí)間數(shù)據(jù)。
DATE_FORMAT(date,format)
//函數(shù)從日期減去指定的時(shí)間間隔。
DATE_SUB(date,INTERVAL expr type)
//函數(shù)返回當(dāng)前的日期。
CURDATE()

例如

SELECT NOW(),CURDATE(),CURTIME()

SQL對(duì)數(shù)據(jù)進(jìn)行按月統(tǒng)計(jì)或?qū)?shù)據(jù)進(jìn)行按星期統(tǒng)計(jì)的實(shí)例代碼

   例如

SELECT DAYOFWEEK(NOW()),WEEKDAY(now()),DATE_FORMAT(NOW(),"%w"),NOW()

SQL對(duì)數(shù)據(jù)進(jìn)行按月統(tǒng)計(jì)或?qū)?shù)據(jù)進(jìn)行按星期統(tǒng)計(jì)的實(shí)例代碼

二、對(duì)積分訂單數(shù)據(jù)表按周匯總統(tǒng)計(jì)訂單量,比如今天是周二,返回周一到周二的每天單量匯總數(shù)據(jù),依次類推

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、數(shù)據(jù)庫(kù)返回如下:

SQL對(duì)數(shù)據(jù)進(jìn)行按月統(tǒng)計(jì)或?qū)?shù)據(jù)進(jìn)行按星期統(tǒng)計(jì)的實(shí)例代碼

3、關(guān)鍵詞

//格式化時(shí)間戳,返回星期數(shù),注意周日返回值為0
DATE_FORMAT(FROM_UNIXTIME(pay_time,'%Y-%m-%d'),'%w')
//返回當(dāng)前時(shí)間為一年中第幾周
WEEK(DATE_ADD(CURDATE(),interval 6 day),2)
//獲取指定日期是一年中的第幾周
WEEK(date,mode)
//函數(shù)向日期添加指定的時(shí)間間隔。
DATE_ADD(date,INTERVAL expr type)
//所以上面的查詢條件為星期和第幾周同時(shí)滿足 

影子是一個(gè)會(huì)撒謊的精靈,它在虛空中流浪和等待被發(fā)現(xiàn)之間;在存在與不存在之間....

總結(jié)

以上所述是小編給大家介紹的SQL對(duì)數(shù)據(jù)進(jìn)行按月統(tǒng)計(jì)或?qū)?shù)據(jù)進(jìn)行按星期統(tǒng)計(jì)的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)創(chuàng)新互聯(lián)網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!


本文名稱:SQL對(duì)數(shù)據(jù)進(jìn)行按月統(tǒng)計(jì)或?qū)?shù)據(jù)進(jìn)行按星期統(tǒng)計(jì)的實(shí)例代碼
網(wǎng)站鏈接:http://weahome.cn/article/ggchci.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部