鍍金池/ 問答/PHP  數(shù)據(jù)庫/ 能不能只用一條sql語句就查詢出不同時(shí)間段(一天,一周,一個(gè)月...)的相同字段

能不能只用一條sql語句就查詢出不同時(shí)間段(一天,一周,一個(gè)月...)的相同字段的統(tǒng)計(jì)結(jié)果?

想做一個(gè)代理后臺(tái)的充值金額統(tǒng)計(jì)表格,分別需要統(tǒng)計(jì)出一天內(nèi)、一周內(nèi)、一個(gè)月內(nèi)用戶充值的總額,但我不想分開用多條sql語句查詢(因?yàn)椴樵兯俣鹊钠款i主要在I/O那里)而想用一條就能查出想要的數(shù)據(jù)這種需求能否實(shí)現(xiàn)?

回答
編輯回答
只愛你

查出每天內(nèi)的,程序不就可以二次處理算出周內(nèi)和月內(nèi)的充值總額了么

2018年3月4日 00:25
編輯回答
蝶戀花
select 
    sum(case when create_date>=date_sub(current_date(),interval 1 day) then amount else 0 end) day_amount,
    sum(case when create_date>=date_sub(current_date(),interval 7 day) then amount else 0 end) week_amount,
    sum(amount) month_amount
from table 
where create_date>=date_sub(current_date(),interval 30 day);

另外說一下,這種需求如果是要實(shí)時(shí)頻繁計(jì)算的話,最好每天生成一條報(bào)表數(shù)據(jù),每次拉取報(bào)表數(shù)據(jù)即可

2018年7月26日 03:50