MySQL查詢今天/本週/上週/本月/上個月份的資料
MySQL查詢的方式很多,下面為您介紹的MySQL查詢實現的是查詢本週、上週、本月、上個月份的資料,如果您對MySQL查詢方面感興趣的話,不妨一看。
查詢當前今天的資料
SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) =date_format(now(),'%Y-%m-%d');
查詢當前這周的資料
SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now())
查詢上週的資料
SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now())-1;
查詢當前月份的資料
select name,submittime from enterprise where date_format(submittime,'%Y-%m')=date_format(now(),'%Y-%m')
查詢距離當前現在6個月的資料
select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now()
查詢上個月的資料
select name,submittime from enterprise where date_format(submittime,'%Y-%m')=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m')
select * from `user` where DATE_FORMAT(pudate,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m');
select * from user where WEEKOFYEAR(FROM_UNIXTIME(pudate,'%y-%m-%d')) = WEEKOFYEAR(now())
select *
from user
where MONTH(FROM_UNIXTIME(pudate,'%y-%m-%d')) = MONTH(now())
select *
from [user]
where YEAR(FROM_UNIXTIME(pudate,'%y-%m-%d')) = YEAR(now())
and MONTH(FROM_UNIXTIME(pudate,'%y-%m-%d')) = MONTH(now())
select *
from [user]
where pudate between 上月最後一天
and 下月第一天
############################
預設情況下, 這個
yearweek、WEEK ( 日期 ) 是 每把週日,作為一週的開始。
有的是週日開始,有的是週一開始。而工作中有的時候每週是從週六開始算的,有些資料需要按周統計,那麼那種方式比較好呢?
通過下面的研究,認為yearweek()是適合的,它返回的是 201311這樣的格式。若要把週六當做新的一週的開始,那麼就是: select yearweek(DATE_ADD(now(), INTERVAL 1 DAY)); mysql> select yearweek(DATE_ADD(now(), INTERVAL 1 DAY)); +-------------------------------------------+ | yearweek(DATE_ADD(now(), INTERVAL 1 DAY)) | +-------------------------------------------+ | 201311 | +-------------------------------------------+ 1 row in set mysql> select yearweek(now()); +-----------------+ | yearweek(now()) | +-----------------+ | 201310 | +-----------------+ 1 row in set mysql>select
yearweek(now() -
INTERVAL 1 DAY) 從周1開始本週計算
select yearweek('2015-02-02 11:12:00' - INTERVAL 1 DAY)
############################
摘自:http://hideto.javaeye.com/blog/255816
當前week的第一天:
select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 1 DAY)
當前week的最後一天:
select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) – 5 DAY)
前一week的第一天:
select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 8 DAY)
前一week的最後一天:
select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 2 DAY)
前兩week的第一天:
select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 15 DAY)
前兩week的最後一天:
select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 9 DAY)
當前month的第一天:
SELECT concat(date_format(LAST_DAY(now()),’%Y-%m-’),’01′)
當前month的最後一天:
SELECT LAST_DAY(now())
前一month的第一天:
SELECT concat(date_format(LAST_DAY(now() – interval 1 month),’%Y-%m-’),’01′)
前一month的最後一天:
SELECT LAST_DAY(now() – interval 1 month)
前兩month的第一天:
SELECT concat(date_format(LAST_DAY(now() – interval 2 month),’%Y-%m-’),’01′)
前兩month的最後一天:
SELECT LAST_DAY(now() – interval 2 month)
當前quarter的第一天:
select concat(date_format(LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-3 month),’%Y-%m-’),’01′)
當前quarter的最後一天:
select LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-1 month)
前一quarter的第一天:
select concat(date_format(LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-6 month),’%Y-%m-’),’01′)
前一quarter的最後一天:
select LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-4 month)
前兩quarter的第一天:
select concat(date_format(LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-9 month),’%Y-%m-’),’01′)
前兩quarter的最後一天:
select LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-7 month)
UNIX時間戳轉換為日期用函式: FROM_UNIXTIME()
- select FROM_UNIXTIME(1156219870);
日期轉換為UNIX時間戳用函式: UNIX_TIMESTAMP()
- Select UNIX_TIMESTAMP(’2006-11-04 12:23:00′);
########################