MySQL 查询大于“时间字段”15分钟、1小时、1天的数据

以下代码中times为时间字段,类型为datetime

1.查询大于times十五分钟的数据

//大于号后面都是获取times十五分钟后的时间
select*from table where now() >SUBDATE(times,interval -15 minute);
select*from table where now() > SUBDATE(times,interval -900 second);
select*from table where now() > date_add(times,interval 15 minute);
select*from table where now() >ADDDATE(times,interval 15 minute);

2.查询大于times一小时的数据

//大于号后面都是获取times一小时后的时间
select*from table where now() >SUBDATE(times,interval -1 hour);
selectfrom table where now() > SUBDATE(times,interval -6060 second);
select*from table where now() > date_add(times,interval -1 hour);
select*from table where now() >ADDDATE(times,interval 15 hour);

3.查询大于times一天的数据

//大于号后面都是获取times一天后的时间
select*from table where now() >SUBDATE(times,interval -1 day);
selectfrom table where now() > SUBDATE(times,interval -6060*60 second);
select*from table where now() > date_add(times,interval -1 day);
select*from table where now() >ADDDATE(times,interval 15 day);

相应的如果想查询前一天的数据,加一个“-”号即可

MYSQL查询大于某个时间的数据
转载weixin_30832405 最后发布于2019-05-22 15:24:00 阅读数 3851 收藏
展开
今天:

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 tb where addTime > '2011-12-11'

select * from tb where convert(varchar(10),addTime,120) > '2011-12-11'

Last modification:April 17, 2020
如果觉得我的文章对你有用,请随意赞赏