1

所以我有这张order桌子

| order_id | sales | order_date(timestamp) |
      2      100      2013-10-30 04:07:01
      3      300      2013-10-31 04:30:02
      4      400      2014-01-10 08:07:01

这是我的查询$m1$m2来自order_date

SELECT `order_id`, MONTH(`order_date`) AS month, 
COUNT( `order_id`) AS orders_made,
SUM(`total`) AS sales, YEAR(`order_date`) AS YEAR
FROM  `orders` 
WHERE  `order_date` BETWEEN ('$m1') AND ('$m2') 
GROUP BY MONTH(`order_date`)

这就是我得到的

| month | year | sales | orders_made |
    10    2013    100        1
    11    2014    400        1

我的查询没有返回我想要的所有数据,应该是什么查询来显示这样的内容

| month | year | sales | orders_made |
    10    2013    400        2
    11    2014    400        1

需要你们的帮助

4

1 回答 1

2
SELECT YEAR(order_date), 
       MONTH(order_date),
       COUNT(order_id) AS orders_made,
       SUM(sales) AS sales 
FROM orders 
WHERE order_date BETWEEN ('$m1') AND ('$m2') 
GROUP BY YEAR(order_date), MONTH(order_date)
于 2013-10-30T21:25:37.987 回答