【发布时间】:2022-01-13 01:40:57
【问题描述】:
我有两个表,其中一个有一个唯一的 ID 列和一个包含数组的列,这些数组包含表 B 中唯一记录的零个或多个标识符。我试图显示每个项目的使用顺序,但是我无法想办法将表 A 和 B 连接起来,这样不仅每个订单或项目都有一行,而且所有订单上都有尽可能多的项目
表 A:
OrderID | Items | name
----------+------------------+------------
order1 | {item1,item2} | "Bob's pizza order"
order2 | {item3,item1} | "Alice's breakfast order"
表 B:
itemID | price | name
---------+----------------+------------
item1 | 2.95 | "cheese"
item2 | 3.15 | "tomato sauce"
item3 | 3.50 | "eggs"
期望的输出类似于
ItemID | OrderID | name
---------+----------------+------------
item1 | order1 | "cheese"
item1 | order2 | "cheese"
item2 | order1 | "tomato sauce"
item3 | order2 | "eggs"
有谁知道如何取消表 A 中的数组的嵌套,以便我可以使用所有项目将 A 和 B 与每个订单的每个项目的记录连接起来?
【问题讨论】:
-
你可以阅读这篇文章:stackoverflow.com/questions/2486725/…
-
更好的解决方案是正确规范您的数据模型。如果您需要定期取消嵌套数组,这强烈表明选择去规范化不是一个好的选择。
标签: sql arrays postgresql join