今天
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
查詢本季度數(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));
查詢當(dāng)前這周的數(shù)據(jù)
SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now());
查詢上周的數(shù)據(jù)
SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now())-1;
查詢上個月的數(shù)據(jù)
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 下月第一天
查詢當(dāng)前月份的數(shù)據(jù)
select name,submittime from enterprise where date_format(submittime,'%Y-%m')=date_format(now(),'%Y-%m')
查詢距離當(dāng)前現(xiàn)在6個月的數(shù)據(jù)
select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();
PS:下面看下mysql如何查詢當(dāng)天信息?
原來不是太熟悉SQL查詢語句,什么都是用到了再去查去找,還好網(wǎng)絡(luò)提供給我們很多支持。今天又用到了一個語句,一時間真想不出怎么解決,到網(wǎng)上看了看,感覺就有一個,怎么那么簡單啊。需要積累的東西真是太多了。
今天就把我這個簡單的問題記錄下來吧!算是一個積累:
mysql查詢當(dāng)天的所有信息:
select * from test where year(regdate)=year(now()) and month(regdate)=month(now()) and day(regdate)=day(now())
這個有一些繁瑣,還有簡單的寫法:
select * from table where date(regdate) = curdate();
查詢本年的所有月份
select concat((select year(now())),'-01') as `date`
union select concat((select year(now())),'-02')
union select concat((select year(now())),'-03')
union select concat((select year(now())),'-04')
union select concat((select year(now())),'-05')
union select concat((select year(now())),'-06')
union select concat((select year(now())),'-07')
union select concat((select year(now())),'-08')
union select concat((select year(now())),'-09')
union select concat((select year(now())),'-10')
union select concat((select year(now())),'-11')
union select concat((select year(now())),'-12')
1.查詢本月第一天
select date_add(curdate(),interval-day(curdate())+1 day) as date;
2.查詢本月最后一天
SELECT last_day(curdate()) as date;
3.查詢當(dāng)前日期
select curdate();
4.查詢下個月的第一天
select date_add(curdate() - day(curdate()) +1,interval 1 month );
5.查詢當(dāng)前月已過了幾天
select day(curdate());
6.獲取當(dāng)前月的天數(shù)(先加一個月,再減今天是第幾天,得到當(dāng)前月的最后一天,最后求最后一天是幾號)
select day(date_add( date_add(curdate(),interval 1 month),interval -day(curdate()) day ));
7.查詢上個月的第一天
select date_sub(date_sub(date_format(now(),‘%y-%m-%d’),interval extract(day from now())-1 day),interval 1 month);
8.查詢上個月的最后一天
select date_sub(date_sub(date_format(now(),’%y-%m-%d’),interval extract(day from now()) day),interval 0 month) as date;
9.查詢當(dāng)月所有日期
select date from (
SELECT DATE_FORMAT(DATE_SUB(last_day(curdate()), INTERVAL xc-1 day), '%Y-%m-%d') as date
FROM (
SELECT @xi:=@xi+1 as xc from
(SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6) xc1,
(SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6) xc2,
(SELECT @xi:=0) xc0
) xcxc) x0 where x0.date >= (select date_add(curdate(),interval-day(curdate())+1 day));
10.查詢某段時間段內(nèi)的所有月份
SELECT
DATE_FORMAT( DATE_ADD( CONCAT( '2023-04', '-01' ), INTERVAL -( CAST( help_topic_id AS SIGNED INTEGER )) MONTH ), '%Y-%m' ) sdate
FROM
mysql.help_topic
WHERE
help_topic_id <= TIMESTAMPDIFF(MONTH,CONCAT( '2022-04', '-01' ),CONCAT( '2023-04', '-01' ))
ORDER BY sdate