从多个表中选择数据列表
Posted
技术标签:
【中文标题】从多个表中选择数据列表【英文标题】:Select data list from multiple tables 【发布时间】:2020-07-04 11:43:44 【问题描述】:Item1 表:
----------------------------------
id | name | price | product_id
----------------------------------
1 | Laptop | 50 | 20
2 | PC | 150 | 10
3 | Notebook| 125 | 25
Item2 表:
----------------------------------
id | name | price | product_id
----------------------------------
1 | Mobile | 50 | 20
2 | Landline| 150 | 10
3 | Pager | 125 | 25
Item3 表:
----------------------------------
id | name | price | product_id
----------------------------------
1 | Canon | 250 | 20
2 | Nikon | 150 | 10
3 | Sony | 325 | 25
主表:
------------------------------------------------------------------
product_id | quantity | manufacturer | shipment | item
------------------------------------------------------------------
20 | 150 | China | Delivery | item1
20 | 200 | India | Delivery | item2
20 | 300 | USA | Pickup | item3
15 | 200 | USA | Pickup | Item2
我想按顺序从所有 3 个项目表中获取项目详细信息(项目 1、项目 2、项目 3)以及基于产品 ID 的主表中项目的产品详细信息(例如:其中产品 ID = 20)像下面的结果:
----------------------------------------------------------------------------
item | name | price | quantity | manufacturer | shipment
----------------------------------------------------------------------------
item1 | Laptop | 50 | 150 | China | Delivery
item2 | Mobile | 50 | 200 | India | Delivery
item3 | Canon | 250 | 300 | USA | Pickup
非常感谢您的帮助!
【问题讨论】:
如何区分每一行指的是哪个表? @GordonLinoff 它通过主表中的 item 列来区分(item1 指的是 Item 1 表,item2 指的是 Item 2 表,依此类推)。谢谢! 【参考方案1】:也许union
可以达到预期的效果?
SELECT m.item, i.name, i.price, m.quantity, m.manufacturer, m.shipment
FROM Master m
INNER JOIN Item1 i ON i.product_id = m.product_id
WHERE m.item = 'item1'
AND m.product_id = 20
UNION
SELECT m.item, i.name, i.price, m.quantity, m.manufacturer, m.shipment
FROM Master m
INNER JOIN Item2 i ON i.product_id = m.product_id
WHERE m.item = 'item2'
AND m.product_id = 20
UNION
SELECT m.item, i.name, i.price, m.quantity, m.manufacturer, m.shipment
FROM Master m
INNER JOIN Item3 i ON i.product_id = m.product_id
WHERE m.item = 'item3'
AND m.product_id = 20
【讨论】:
【参考方案2】:你可以使用一系列left join
s:
SELECT m.item,
COALESCE(i1.name, i2.name, i3.name) as name,
COALESCE(i1.price, i2.price, i3.price) as price,
m.quantity, m.manufacturer, m.shipment
FROM Master m LEFT JOIN
Item1 i1
ON i1.product_id = m.product_id AND
m.item = 'item1' LEFT JOIN
Item1 i2
ON i2.product_id = m.product_id AND
m.item = 'item2' LEFT JOIN
Item1 i1
ON i3.product_id = m.product_id AND
m.item = 'item3'
WHERE m.product_id = 20;
但是,我建议您将它们结合在一起:
SELECT m.item,
COALESCE(i1.name, i2.name, i3.name) as name,
COALESCE(i1.price, i2.price, i3.price) as price,
m.quantity, m.manufacturer, m.shipment
FROM Master m LEFT JOIN
((SELECT 'item1' as item, i1.*
FROM Item1 i1
) UNION ALL
(SELECT 'item2' as item, i2.*
FROM Item1 i2
) UNION ALL
(SELECT 'item3' as item, i3.*
FROM Item1 i3
)
) i
ON i.product_id = m.product_id AND
i.item = m.item
WHERE m.product_id = 20;
我建议您将“项目”全部存储在一个表中,以方便此类查询。
【讨论】:
以上是关于从多个表中选择数据列表的主要内容,如果未能解决你的问题,请参考以下文章