我有一个表 struct 如下:
store 排行榜:
id | shop_name |
---|---|
1 | My Shop |
2 | Another Shop |
3 | Final Shop |
订单表:
shop_id | order_id | data | created_at |
---|---|---|---|
1 | 123 | json | 01/01/2023 |
2 | 456 | json | 01/01/2023 |
3 | 234 | json | 02/02/2023 |
1 | 765 | json | 03/02/2023 |
2 | 435 | json | 04/03/2023 |
3 | 788 | json | 04/03/2023 |
从这两个表中,我想 Select store 名称、ID和按创建月份分组的订单计数,如下所示:
shop_id | shop_name | order_count_january_23 | order_count_february_23 | etc |
---|---|---|---|---|
1 | My Shop | 1 | 2 | ... |
2 | Another Shop | 12 | 45 | ... |
3 | Final Shop | 8 | 36 | ... |
我已经try 对订单数量进行分组,但似乎无法获得所需的确切输出,我还try 使用子查询,但无济于事.
SELECT orders.id,
shops.name,
count(orders.id) as 'Orders',
MONTH(orders.created_at) as Month,
YEAR(orders.created_at) as Year
FROM orders
JOIN shops on shops.id = orders.id
GROUP BY orders.wholesaler_id, Year, Month
我知道上面的方法不起作用,但这就是我被难住的地方.然而,它确实给了我一份具有三个订单编号的store 列表,但格式不正确
任何帮助我们都将不胜感激.