gpt4 book ai didi

mysql - 透视 MySQL 数据

转载 作者:行者123 更新时间:2023-11-30 22:21:45 27 4
gpt4 key购买 nike

我希望我可以更改架构,但我受制于它,假设我有以下表格

JanDataTable
FebDataTable
MarDataTable
ProductsTable

其中前三个表有 ID 和 Amount Sold 列。产品表有 ID 和产品名称。

我需要以某种方式连接这些表以获得以下输出

ID | Product Name | Amount Sold | Month 
01 | TV 1 | 23 | 1 <- JanDataTable
02 | TV 2 | 43 | 2 <- FebDataTable
03 | Toaster 1 | 72 | 3 <- MarDataTable

其中 ID 是产品 ID。

可以只查询吗?

最佳答案

select p.product_part,s.*
from
(
select d.*, 1 as Month from jandatatable d
union
select d.*, 2 as Month from febdatatable d
union
select d.*, 3 as Month from mardatatable d
) s
join product p on s.Product_id = p.product_id
order by s.month, p.product_id;

关于mysql - 透视 MySQL 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36490719/

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