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);
select*from table where now() > SUBDATE(times,interval -60*60 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);
select*from table where now() > SUBDATE(times,interval -60*60*60 second);
select*from table where now() > date_add(times,interval -1 day);
select*from table where now() >ADDDATE(times,interval 15 day);

相應的如果想查詢前一天的數據,加一個“-”號即可

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章