>  기사  >  데이터 베이스  >  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으로 문의하세요.