mysql-MYsql 數(shù)據(jù)庫 ,有個sql如何寫

mysqljava數(shù)據(jù)庫

當前有張news表 有字段 addtime ,name ,
查詢當前時間起,24小時前的數(shù)據(jù),時間不知怎么寫。

回復內(nèi)容:

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)

4 查詢近30天的信息記錄:

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

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,當然,前提是日期格式相同

? 版權聲明
THE END
喜歡就支持一下吧
點贊13 分享