gpt4 book ai didi

MySQL DATE_ADD 用法,5 天间隔

转载 作者:IT老高 更新时间:2023-10-29 00:06:54 24 4
gpt4 key购买 nike

我正在尝试在单个查询中选择 5 天内的订单总金额 ($) 和发票计数。不过,我似乎无法做到这一点。我当前的查询在这里...

SELECT
COUNT(id) as invoice_count,
SUM(orderTotal) as orders_sum,
UNIX_TIMESTAMP(created) as created
FROM ids_invoice
WHERE DATE_ADD(created, INTERVAL +1 DAY)
AND userId = 23 LIMIT 5'

我不完全确定 DATE_ADD 是我正在寻找的正确函数。

目前我正在......

Array ( 
[0] => Array (
[invoice_count] => 420
[orders_total] => 97902.90
[created] => 1252596560
)
)

Array (
[0] => Array (
[invoice_count] => 68
[orders_total] => 14193.20
[created] => 1262900809
)
)

我想要一些更像...的东西

Array ( 
[0] => Array (
[invoice_count] => 18
[orders_total] => 4902.90
[date] => 04-19-2010
)
)

Array (
[0] => Array (
[invoice_count] => 12
[orders_total] => 5193.20
[date] => 04-20-2010
)
)

我对 mysql 日期函数还很陌生,所以也许我在浏览 mysql 文档时错过了我需要的函数。

更新我已经更新了我的查询......这仍然没有为有发票的每一天拉一行。只有当 20 日有符合 userId 条件的发票时,才会提取 19 日的发票。

SELECT
COUNT(id) as invoice_count,
SUM(orderTotal) as orders_sum,
UNIX_TIMESTAMP(created) as created
FROM ids_invoice
WHERE
created BETWEEN "2010-04-19 00:00:00" AND DATE_ADD("2010-04-19 00:00:00", INTERVAL +5 DAY) AND
userId = 17

最佳答案

要获取日期跨度之间的记录,请使用:

WHERE created BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 5 DAY)

此示例将为您获取从今天到 future 几天的记录(假设今天存在任何记录,包括时间)。如果您想回到过去,请查看 DATE_SUB。

关于MySQL DATE_ADD 用法,5 天间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2684520/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com