查询当天数据
1 | select * from tab where FROM_UNIXTIME(fabutime, '%Y%m%d') = 20121217; |
mysql TO_DAYS(date) 函数
TO_DAYS(date)
给定一个日期date, 返回一个天数 (从年份0开始的天数 )。
1
2
3mysql> SELECT TO_DAYS(950501);
-> 728779
mysql查询今天、昨天、7天、近30天、本月、上一月 数据
今天1
2
3
4
5select * from 表名 where to_days(时间字段名) = to_days(now());
```
昨天
``` bash
SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) - TO_DAYS( 时间字段名) <= 1
7天1
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(时间字段名)
近30天1
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(时间字段名)
本月1
SELECT * FROM 表名 WHERE DATE_FORMAT( 时间字段名, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )
上一月1
SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( 时间字段名, '%Y%m' ) ) =1
#查询本季度数据1
select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(now());
#查询上季度数据1
select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));
#查询本年数据1
select * from `ht_invoice_information` where YEAR(create_date)=YEAR(NOW());
#查询上年数据1
select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));
更新某个字段中存在某字符串
1 | UPDATE `web` SET `header_img`=REPLACE(`url`, 'httpss', 'https') WHERE id IN (SELECT aid FROM (SELECT id AS aid FROM `web` WHERE LOCATE('httpss://',`url`)>0 ) AS a ); |