在 SQL 中连接水平表和垂直表的最佳方法是什么? (即在本例中,连接两个表的 ItemID)
我有一个像这样的表:
另一个表是这样的:
理想情况下,我想将它们排列成这样:
订单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/