mysql查询用户每年每月的订单数量及订单金额
可以使用如下的 SQL 查询语句来查询用户每年每月的订单数量及订单金额:
SELECT
YEAR(order_date) AS year,
MONTH(order_date) AS month,
COUNT(*) AS order_count,
SUM(order_amount) AS order_total
FROM
orders
WHERE
user_id = [用户ID]
GROUP BY
YEAR(order_date),
MONTH(order_date)
ORDER BY
YEAR(order_date),
MONTH(order_date)
该查询语句会返回每年每月的订单数量和订单金额,以及用户ID,按照年份和月份进行分组,并按时间顺序排序。您可以将查询结果用于图表的制作,并绘制出用户的订单数量和订单金额的趋势图。请注意,在查询中,您需要将 [用户ID]
替换为您需要查询的用户的ID。
如果order_date为时间戳,可以使用FROM_UNIXTIME
处理一下
SELECT
YEAR(FROM_UNIXTIME(order_date)) AS year,
MONTH(FROM_UNIXTIME(order_date)) AS month,
COUNT(*) AS order_count,
SUM(order_amount) AS order_total
FROM
orders
WHERE
user_id = [用户ID]
GROUP BY
YEAR(FROM_UNIXTIME(order_date)),
MONTH(FROM_UNIXTIME(order_date))
ORDER BY
YEAR(FROM_UNIXTIME(order_date)),
MONTH(FROM_UNIXTIME(order_date))