查詢本周記錄
成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供廣陵企業(yè)網(wǎng)站建設,專注與成都網(wǎng)站設計、網(wǎng)站制作、外貿(mào)營銷網(wǎng)站建設、H5頁面制作、小程序制作等業(yè)務。10年已為廣陵眾多企業(yè)、政府機構等服務。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進行中。
select * from ht_invoice_information where WEEKOFYEAR(create_date)=WEEKOFYEAR(NOW());
#查詢上周記錄
select * from ht_invoice_information where create_date=date_add(now(),interval -(8 + weekday(now())) day)
and create_date=date_add(now(),interval -(1 + weekday(now())) day);
#或者
select * from `ht_invoice_information` where WEEKOFYEAR(create_date)=WEEKOFYEAR(DATE_SUB(now(),INTERVAL 1 week));
#查詢本月數(shù)據(jù)
select * from ht_invoice_information where MONTH(create_date)=MONTH(NOW()) and year(create_date)=year(now());
#查詢上月數(shù)據(jù)
select * from ht_invoice_information where create_date=last_day(date_add(now(),interval -1 MONTH))
and create_date=DATE_FORMAT(concat(extract(year_month from date_add(now(),interval -1 MONTH)),'01'),'%Y-%m-%d');
#或者
select * from `ht_invoice_information` where MONTH(create_date)=MONTH(DATE_SUB(NOW(),interval 1 month))
and year(create_date)=year(now());
#查詢本季度數(shù)據(jù)
select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(now());
#查詢上季度數(shù)據(jù)
select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));
#查詢本年數(shù)據(jù)
select * from `ht_invoice_information` where YEAR(create_date)=YEAR(NOW());
#查詢上年數(shù)據(jù)
select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));
參考這個吧,很全了。
select curDate(); #獲取當前日期select curTime(); #獲取當前時間select now(); #獲取當前日期+時間
列舉1個天數(shù)加減的例子,其他的看英文意思就可以理解了
select date_add(now(), interval 1 day); #當前日期天數(shù)+1
select date_add(now(), interval -1 day); #當前日期天數(shù)-1
select date_add(now(), interval 1 hour);
select date_add(now(), interval 1 minute);
select date_add(now(), interval 1 second);
select date_add(now(), interval 1 microsecond);
select date_add(now(), interval 1 week);
select date_add(now(), interval 1 month);
select date_add(now(), interval 1 quarter);
select date_add(now(), interval 1 year);
本季度的第一天 ,然后你可以把字符串再轉換為日期。 方法很多,包括同一個函數(shù)的別名(同義詞)也很多。比如CURDATE(),CURRENT_DATE(), CURRENT_DATE, NOW 等都可以返回當天
mysql教程 select CURDATE(),ELT(QUARTER(CURDATE()),
- year(CURDATE())*1000+0101,
- year(CURDATE())*1000+0401,
- year(CURDATE())*1000+0701,
- year(CURDATE())*1000+1001) as firstDayofQ;
+------------+-------------+
| CURDATE() | firstDayofQ |
+------------+-------------+
| 2009-05-19 | 2009401 |
+------------+-------------+
1 row in set (0.00 sec)
mysql
用mysql語句獲取本季度的第一天
本月的第一天,
date(
concat(year(curdate()),'-',month(curdate()),'-','1'))
本周的第一天
curdate()-
WEEKDAY(curdate())
用mysql語句獲取本季度的第一天
您好,一、年度查詢
查詢 本年度的數(shù)據(jù)
SELECT *
FROM blog_article
WHERE year( FROM_UNIXTIME( BlogCreateTime ) ) = year( curdate( ))
二、查詢季度數(shù)據(jù)
查詢數(shù)據(jù)附帶季度數(shù)
SELECT ArticleId, quarter( FROM_UNIXTIME( `BlogCreateTime` ) )
FROM `blog_article`
其他的同前面部分:查詢 本季度的數(shù)據(jù)
SELECT *
FROM blog_article
WHERE quarter( FROM_UNIXTIME( BlogCreateTime ) ) = quarter( curdate( ))
三、查詢月度數(shù)據(jù)
本月統(tǒng)計(MySQL)
select * from booking where month(booking_time) =
month(curdate()) and year(booking_time) = year(curdate())
本周統(tǒng)計(MySQL)
select * from spf_booking where month(booking_time) =
month(curdate()) and week(booking_time) = week(curdate())
四、時間段
N天內(nèi)記錄
WHERE TO_DAYS(NOW()) - TO_DAYS(時間字段) = N
當天的記錄
where date(時間字段)=date(now())
或
where to_days(時間字段) = to_days(now());
查詢一周:
select * from table where DATE_SUB(CURDATE(), INTERVAL 7 DAY) = date(column_time);
查詢一個月:
select * from table where DATE_SUB(CURDATE(), INTERVAL INTERVAL 1 MONTH) = date(column_time);
查詢'06-03'到'07-08'這個時間段內(nèi)所有過生日的會員:
Select * From user Where
DATE_FORMAT(birthday,'%m-%d') = '06-03' and DATE_FORMAT(birthday,'%m-%d')
= '07-08';
統(tǒng)計一季度數(shù)據(jù),表時間字段為:savetime
group by concat(date_format(savetime, '%Y '),FLOOR((date_format(savetime, '%m ')+2)/3))
或
select YEAR(savetime)*10+((MONTH(savetime)-1) DIV 3) +1,count(*)
from yourTable
group by YEAR(savetime)*10+((MONTH(savetime)-1) DIV 3) +1;
五、分組查詢
1、年度分組
2、月度分組
3、先按年度分組,再按月度分組
4、按年月分組
SELECT count(ArticleId), date_format(FROM_UNIXTIME( `BlogCreateTime`),'%y%m') sdate FROM `blog_article` group by sdate
結果:
count( ArticleId ) sdate
17 0901
11 0902
5 0903
6 0904
2 0905
1 0907
12 0908
6 0909
11 0910
3 0911
倆方法
select?year(訂單.訂購日期)年份,
sum(case?when?month(訂單.訂購日期)?between?1?and?3?then?訂單明細.單價*訂單明細.數(shù)量?else?0?end)?一季度銷售金額,
sum(case?when?month(訂單.訂購日期)?between?4?and?6?then?訂單明細.單價*訂單明細.數(shù)量?else?0?end)?二季度銷售金額,
sum(case?when?month(訂單.訂購日期)?between?7?and?9?then?訂單明細.單價*訂單明細.數(shù)量?else?0?end)?三季度銷售金額,
sum(case?when?month(訂單.訂購日期)?between?10?and?12?then?訂單明細.單價*訂單明細.數(shù)量?else?0?end)?四季度銷售金額
from?訂單,訂單明細?
where?訂單.訂單ID=訂單明細.訂單ID?and?year(訂單.訂購日期)?between?1996?and?1998
group?by?year(訂單.訂購日期)
select?year(訂單.訂購日期)?年份,
case?when?month(訂單.訂購日期)?between?1?and?3?then?'一季度'
when?month(訂單.訂購日期)?between?4?and?6?then?'二季度'
when?month(訂單.訂購日期)?between?7?and?9?then?'三季度'
when?month(訂單.訂購日期)?between?10?and?12?then?'四季度'?end?季度,
sum(訂單明細.單價*訂單明細.數(shù)量)?金額
from?訂單,訂單明細?
where?訂單.訂單ID=訂單明細.訂單ID?and?year(訂單.訂購日期)?between?1996?and?1998
group?by?year(訂單.訂購日期),
case?when?month(訂單.訂購日期)?between?1?and?3?then?'一季度'
when?month(訂單.訂購日期)?between?4?and?6?then?'二季度'
when?month(訂單.訂購日期)?between?7?and?9?then?'三季度'
when?month(訂單.訂購日期)?between?10?and?12?then?'四季度'?end
你看你要用哪個