如何使用MySQL中的datediff函数过滤行?

时间:2021-07-20 01:36:11

I have a table with following structure:

我有一张表,它的结构如下:

+===========+============+
| titleName |    date    |
+===========+============+
|  title1   | 2016-08-06 |
+===========+============+
|  title2   | 2016-08-03 |
+===========+============+
|  title3   | 2016-07-29 |
+===========+============+
|  title4   | 2016-08-09 |
+===========+============+
|  title5   | 2016-09-03 |
+===========+============+

I want to select only the rows that are not older than 90 days from the current date (2016-11-04) as follows:

我只想从当前日期(2016-11-04)中选择不超过90天的行如下:

+===========+============+
| titleName |    date    |
+===========+============+
|  title4   | 2016-08-09 |
+===========+============+
|  title5   | 2016-09-03 |
+===========+============+

I have tried the following query, but it doesn't work as expected:

我尝试过下面的查询,但是没有达到预期的效果:

SELECT titleName, Datediff(NOW(), publish_date) AS datediff FROM myTable ORDER BY titleName DESC LIMIT 8

Please help me to make the correct query.

请帮助我做正确的查询。

3 个解决方案

#1


1  

Use the WHERE clause with an INTERVAL:

使用WHERE子句的间隔:

SELECT * FROM the_table WHERE `date` + INTERVAL 90 DAY >= NOW();

#2


2  

It will work fine

它将正常工作

SELECT titleName, date FROM mytable WHERE Datediff(NOW(), date)<90 ORDER BY titleName DESC LIMIT 8

选择titleName,日期来自mytable,其中Datediff(NOW(), date)<90 ORDER BY titleName DESC LIMIT 8

#3


1  

I think you want this:

我想你想要的是:

select t.*
from mytable t
where publish_date >= date_sub(curdate(), interval 90 day);

If you want to see the number of days, then include the datediff() expression in the select.

如果您想查看天数,那么在select中包含datediff()表达式。

#1


1  

Use the WHERE clause with an INTERVAL:

使用WHERE子句的间隔:

SELECT * FROM the_table WHERE `date` + INTERVAL 90 DAY >= NOW();

#2


2  

It will work fine

它将正常工作

SELECT titleName, date FROM mytable WHERE Datediff(NOW(), date)<90 ORDER BY titleName DESC LIMIT 8

选择titleName,日期来自mytable,其中Datediff(NOW(), date)<90 ORDER BY titleName DESC LIMIT 8

#3


1  

I think you want this:

我想你想要的是:

select t.*
from mytable t
where publish_date >= date_sub(curdate(), interval 90 day);

If you want to see the number of days, then include the datediff() expression in the select.

如果您想查看天数,那么在select中包含datediff()表达式。