首頁  >  文章  >  資料庫  >  mysql-MYsql 資料庫 ,有個sql如何寫

mysql-MYsql 資料庫 ,有個sql如何寫

WBOY
WBOY原創
2016-08-04 08:53:381040瀏覽

mysqljava資料庫

當前有張news表 有字段 addtime ,name ,
查詢當前時間起,24小時前的數據,時間不知怎​​麼寫。

回覆內容:

1查詢今天的資訊記錄:

1 select * from article where to_days(add_time) = to_days(now());

2查詢昨天的資料記錄:

2 select * from article where to_days(now()) – to_days(add_time)

3 查詢近7天的資訊記錄:

3 select * from article where date_sub(curdate(), INTERVAL 7 DAY) add_time);

4 查詢近30天的資訊記錄:

4 select * from article where date_sub(curdate(), INTERVAL 30 DAY) add_time);

5查詢本月的資料記錄:

5 select * from article where date_format(add_time, ‘%Y%m') = date_format(curdate() , ‘%Y%m');

6 查詢上一月的資訊記錄:

6 select * from article where period_diff(date_format(now() , ‘%Y%m') , date_format(add_time, ‘%Y%m')) =1;

select * from news where datediff(year,addtime,getdate())

或select * from news where addtime> sysdate-1

不需要考慮這個addtime的日期類型嗎?
想法是把sysdate-addtime>1,當然,前提是日期格式相同

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn