在mysql时间戳上选择日期范围

时间:2021-07-07 15:31:33

I am trying the following but get no results:

我正在尝试以下但没有得到任何结果:

SELECT *
FROM users_test
WHERE dateadded >= UNIX_TIMESTAMP('2012-02-01 00:00:00') 
AND dateadded <  UNIX_TIMESTAMP('2012-11-01 00:00:00');

Yet I know there are columns with dates within that range e.g.

但我知道有一些日期在该范围内的列,例如

2012-05-11 17:10:08

Is there a better way to do this?

有一个更好的方法吗?

Eventually I want to search multiple parameters, albeit not at the same time, like today, yesterday, last week, last month etc and also a date range and month range

最终我想搜索多个参数,虽然不是在同一时间,如今天,昨天,上周,上个月等,还有日期范围和月份范围

4 个解决方案

#1


13  

Have you tried?

你有没有尝试过?

SELECT *
FROM users_test
WHERE dateadded >= '2012-02-01 00:00:00'
AND dateadded <  '2012-11-01 00:00:00'

For what I can see, it seems your table has the data stored in the same way you want to look for it (2012-05-11 17:10:08), so in this case you won't need UNIX_TIMESTAMP.

对于我所看到的,似乎你的表以你想要的方式存储数据(2012-05-11 17:10:08),所以在这种情况下你不需要UNIX_TIMESTAMP。

Also I can see you want to exclude the 2nd date from results (because you're using < instead of <=), otherwise using WHERE dateadded BETWEEN '2012-02-01 00:00:00' AND '2012-11-01 00:00:00' would be fine as well...

另外我可以看到你想从结果中排除第二个日期(因为你使用的是 <而不是<=),否则使用where dateadded between'2012-02-01 00:00:00'和'2012-11-01 00:00:00'也没问题......< p>

#2


5  

Just use the SQL BETWEEN keyword. That's all.

只需使用SQL BETWEEN关键字即可。就这样。

#3


3  

try this:

SELECT * FROM 
      users_test
 WHERE
     dateadded BETWEEN '2012-02-01 00:00:00' AND '2012-11-01 00:00:00'

#4


0  

SELECT * FROM table_name WHERE DATE(date_field) between '2015-05-10' and '2015-05-21`

#1


13  

Have you tried?

你有没有尝试过?

SELECT *
FROM users_test
WHERE dateadded >= '2012-02-01 00:00:00'
AND dateadded <  '2012-11-01 00:00:00'

For what I can see, it seems your table has the data stored in the same way you want to look for it (2012-05-11 17:10:08), so in this case you won't need UNIX_TIMESTAMP.

对于我所看到的,似乎你的表以你想要的方式存储数据(2012-05-11 17:10:08),所以在这种情况下你不需要UNIX_TIMESTAMP。

Also I can see you want to exclude the 2nd date from results (because you're using < instead of <=), otherwise using WHERE dateadded BETWEEN '2012-02-01 00:00:00' AND '2012-11-01 00:00:00' would be fine as well...

另外我可以看到你想从结果中排除第二个日期(因为你使用的是 <而不是<=),否则使用where dateadded between'2012-02-01 00:00:00'和'2012-11-01 00:00:00'也没问题......< p>

#2


5  

Just use the SQL BETWEEN keyword. That's all.

只需使用SQL BETWEEN关键字即可。就这样。

#3


3  

try this:

SELECT * FROM 
      users_test
 WHERE
     dateadded BETWEEN '2012-02-01 00:00:00' AND '2012-11-01 00:00:00'

#4


0  

SELECT * FROM table_name WHERE DATE(date_field) between '2015-05-10' and '2015-05-21`