gpt4 book ai didi

mysql - SQL中连接垂直表和水平表的最佳方法

转载 作者:行者123 更新时间:2023-11-29 11:25:13 25 4
gpt4 key购买 nike

在 SQL 中连接水平表和垂直表的最佳方法是什么? (即在本例中,连接两个表的 ItemID)

我有一个像这样的表:

enter image description here

另一个表是这样的:

Table 2

理想情况下,我想将它们排列成这样:

订单ID 项目ID 客户ID 描述 数量 价格

根据 OrderID 连接这两个不同类型的表的最简单方法是什么?

现在我首先将项目表从垂直转换为水平,然后连接两个表。但这会变成很多行代码。只是想知道是否有更简单的方法。谢谢!

最佳答案

垂直表上的多个内连接

 select a.OrderID, a.ItemID, a.ClientID, b.Description c.Quantity d.Price
from table2 as a
inner join table1 as b on a.itemID = b.itemID and b.element = 'Description'
inner join table1 as c on a.itemID = c.itemID and c.element = 'Quantity'
inner join table1 as d on a.itemID = d.itemID and d.element = 'Price'

关于mysql - SQL中连接垂直表和水平表的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38336624/

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