语法以返回给定时间间隔的结果?

发布于 2025-02-08 04:33:08 字数 252 浏览 1 评论 0原文

我们只是从MySQL切换到Postgres 13.6。

我正在尝试返回从发票创建日期开始30至59天之间的未付发票的结果,因此自创建以来未支付30天和59天的一切。

在MySQL中,这是这样做的:

and datediff(current_date,date(`invoices`.`created_at`)) BETWEEN  30 AND 59;

我尝试了一些不同的事情,之间的 ,但似乎无效。

We just switched from MySQL to Postgres 13.6.

I am trying to return results for unpaid invoices that are between 30 and 59 days from invoice create date, so anything that has gone 30 and 59 days unpaid since being created.

In MySQL this was done as:

and datediff(current_date,date(`invoices`.`created_at`)) BETWEEN  30 AND 59;

I've tried a few different things with intervals and BETWEEN, but nothing seems to work.

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

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

发布评论

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

评论(1

最初的梦 2025-02-15 04:33:08

请确保使用a sargable 表达式

WHERE invoices.created_at BETWEEN current_date - 59
                              AND current_date - 30

表达式减去整数到日期/从日期开始。示例:

上限和上限的定义是模糊的。不确定您要如何绕上限和下限,并且不确定您是否知道current_date取决于当前会话的timezone设置。我也不相信您的MySQL表达式完全在边缘上做了您想要的...

Be sure to use a sargable expression like:

WHERE invoices.created_at BETWEEN current_date - 59
                              AND current_date - 30

Postgres can add/subtract integer to/from date. Example:

The definition of lower and upper bound are fuzzy. Not sure how you want to round upper and lower bound, and not sure you are aware that current_date depends on the timezone setting of your current session. I am also not confident your MySQL expression did exactly what you wanted around the edges ...

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