Home > Database > Mysql Tutorial > body text

mysql-MYsql database, how to write sql

WBOY
Release: 2016-08-04 08:53:38
Original
1081 people have browsed it

mysqljava database

Currently there is a news table with fields addtime, name,
Query the data 24 hours ago from the current time. I don’t know how to write the time.

Reply content:

1 Query today’s information records:

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

2 Query yesterday’s information records:

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

3 Query the information records of the past 7 days:

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

4 Query the information records of the past 30 days:

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

5 Query this month’s information records:

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

6 Query the information records of the previous month:

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()) <= 1

Or select * from news where addtime> sysdate-1

Don’t you need to consider the date type of this addtime?
The idea is to set sysdate-addtime>1. Of course, the premise is that the date format is the same

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template