我正在尝试在单个查询中选择 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