MySQL 中日期和 BETWEEN 的奇怪之处

发布于 2024-10-16 23:25:23 字数 553 浏览 3 评论 0原文

我正在运行一个查询来获取特定日期之间的行(本例中为当前日期和 7 天前)。

我尝试将其写为:

SELECT * 
FROM faulttracker.ft_v_cases 
WHERE DATE(cs_created) BETWEEN DATE(NOW()) AND DATE(NOW()-INTERVAL 7 DAY) 
ORDER BY cs_created DESC;

但它返回 0 行。我不明白为什么它不起作用,我尝试将其重写为:

SELECT * 
FROM faulttracker.ft_v_cases 
WHERE DATE(cs_created) <= DATE(NOW()) 
AND DATE(cs_created) >= DATE(NOW()-INTERVAL 7 DAY) 
ORDER BY cs_created DESC;

哪个确实起作用。

为什么第一个返回 0 行,但第二个按预期工作?据我所知,它们在功能上应该是等效的。

cs_created 是一个日期时间。

I'm running a query to get rows that fall between certain dates (the current date and 7 days ago for this example).

I tried writing it as:

SELECT * 
FROM faulttracker.ft_v_cases 
WHERE DATE(cs_created) BETWEEN DATE(NOW()) AND DATE(NOW()-INTERVAL 7 DAY) 
ORDER BY cs_created DESC;

but it returned 0 rows. I couldn't understand why it didn't work, and I tried rewriting it as:

SELECT * 
FROM faulttracker.ft_v_cases 
WHERE DATE(cs_created) <= DATE(NOW()) 
AND DATE(cs_created) >= DATE(NOW()-INTERVAL 7 DAY) 
ORDER BY cs_created DESC;

which DID work.

Why did the first one return 0 rows but the second one work as expected? As far as I can see they should be functionally equivalent.

cs_created is a datetime.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

瞎闹 2024-10-23 23:25:23

根据 文档,BETWEEN 期望以下内容格式:

 expr BETWEEN min AND max

在第一个示例中,您将最小值放在最后。

尝试使用:

SELECT * 
FROM faulttracker.ft_v_cases 
WHERE DATE(cs_created) BETWEEN DATE(NOW()-INTERVAL 7 DAY) AND DATE(NOW()) 
ORDER BY cs_created DESC;

According to the documentation, BETWEEN expects the following format:

 expr BETWEEN min AND max

In your first example, you are putting the min value last.

Try using:

SELECT * 
FROM faulttracker.ft_v_cases 
WHERE DATE(cs_created) BETWEEN DATE(NOW()-INTERVAL 7 DAY) AND DATE(NOW()) 
ORDER BY cs_created DESC;
孤者何惧 2024-10-23 23:25:23

我很确定您也可以使用:

WHERE cs_created >= CURDATE() - INTERVAL 7 DAY 
  AND cs_created <  CURDATE() + INTERVAL 1 DAY

这应该返回相同的结果,但也允许查询使用 cs_created 上的索引。

I'm pretty sure you can just as well use:

WHERE cs_created >= CURDATE() - INTERVAL 7 DAY 
  AND cs_created <  CURDATE() + INTERVAL 1 DAY

This should return same results, but also allow the query to use index on cs_created.

可遇━不可求 2024-10-23 23:25:23

您的条件应该是 -

WHERE DATE(cs_created) BETWEEN DATE(NOW()-INTERVAL 7 DAY) AND DATE(NOW())

使用 Between 时,较低的日期值应位于左侧。

Your condition should be -

WHERE DATE(cs_created) BETWEEN DATE(NOW()-INTERVAL 7 DAY) AND DATE(NOW())

The lower date value should be on left side when using Between.

软糖 2024-10-23 23:25:23

您必须始终将范围设置为BETWEEN更小和更大;无论您是否编写,处理都是相同的:

x BETWEEN smaller AND larger
x >= smaller AND x <= larger

因此,使用 BETWEEN 的代码需要编写为:

SELECT * 
  FROM faulttracker.ft_v_cases 
 WHERE DATE(cs_created) BETWEEN DATE(NOW() - INTERVAL 7 DAY) AND DATE(NOW())
 ORDER BY cs_created DESC;

当然,这涵盖了过去 8 天,因为 BETWEEN/AND 的范围包含在内。

You must always have the range working as BETWEEN smaller AND larger; the treatment is the same regardless of whether you write:

x BETWEEN smaller AND larger
x >= smaller AND x <= larger

Hence, your code using BETWEEN needs to be written as:

SELECT * 
  FROM faulttracker.ft_v_cases 
 WHERE DATE(cs_created) BETWEEN DATE(NOW() - INTERVAL 7 DAY) AND DATE(NOW())
 ORDER BY cs_created DESC;

That covers the last 8 days, of course, since the range with BETWEEN/AND is inclusive.

沫雨熙 2024-10-23 23:25:23

您是否尝试过这样写:

SELECT *
来自 failuretracker.ft_v_cases
CAST(NOW() AS DATE) 和 CAST(DATE(NOW()-INTERVAL 7 DAY) AS DATE) 之间的 CAST(cs_created AS DATE)
ORDER BY cs_created DESC;

手册状态:

要在将 BETWEEN 与日期或时间值一起使用时获得最佳结果,请使用 CAST() 将值显式转换为所需的数据类型。

have you tried putting it like this:

SELECT *
FROM faulttracker.ft_v_cases
WHERE CAST(cs_created AS DATE) BETWEEN CAST(NOW() AS DATE) AND CAST(DATE(NOW()-INTERVAL 7 DAY) AS DATE)
ORDER BY cs_created DESC;

the manual states:

to achieve best results when using BETWEEN with date or time values, use CAST() to explicitly convert the values to the desired data type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文