程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> MySql日期查詢語句詳解

MySql日期查詢語句詳解

編輯:MySQL綜合教程

MySql日期查詢語句詳解。本站提示廣大學習愛好者:(MySql日期查詢語句詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是MySql日期查詢語句詳解正文


應用DATE_FORMAT辦法
SELECT * FROM `ler_items` WHERE DATE_FORMAT(postTime,'%Y-%m')='2013-03'
留意:日期必定要用'',不然沒有用果
其它的一些關於mysql日期查找語句
mysql> select date_format(DATE_SUB(CURDATE(), INTERVAL 7 DAY),'%y%m%d');
+———————————————————–+
| date_format(DATE_SUB(CURDATE(), INTERVAL 7 DAY),'%y%m%d') |
+———————————————————–+
| 120416 |
+———————————————————–+
1 row in set (0.00 sec)

可以獲得7天前的日期,
應用DATE_SUB(CURDATE(), INTERVAL 7 DAY) 來取得7天前的時光,用date_format 來指定輸入格局。

明天
select * from 表名 where to_days(時光字段名) = to_days(now());
昨天
SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) – TO_DAYS( 時光字段名) <= 1
7天
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(時光字段名)
近30天
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(時光字段名)
本月
SELECT * FROM 表名 WHERE DATE_FORMAT( 時光字段名, ‘%Y%m' ) = DATE_FORMAT( CURDATE( ) , ‘%Y%m' )
上一月
SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , ‘%Y%m' ) , date_format( 時光字段名, ‘%Y%m' ) ) =1
#查詢本季度數據
select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(now());
#查詢上季度數據
select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));
#查詢今年數據
select * from `ht_invoice_information` where YEAR(create_date)=YEAR(NOW());
#查詢上年數據
select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));

查詢以後這周的數據
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 下月第一天
where date(regdate) = curdate();
select * from test where year(regdate)=year(now()) and month(regdate)=month(now()) and day(regdate)=day(now())
SELECT date( c_instime ) ,curdate( )
FROM `t_score`
WHERE 1
LIMIT 0 , 30
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved