Mysql加入和联盟

时间:2015-10-13 23:30:07

标签: mysql

我有4个架构

Product(maker, model, type)
PC(model, speed, ram, hd, price)
Laptop(model, speed, ram, hd, screen, price)
Printer(model, color, type, price)

需要查找某制造商生产的所有产品的型号和价格。如果能够使用

获得所有不同产品的价格和型号
(select model, price
from Laptop)
union
(select model, price
from PC)
union
(select model, price
from Printer)

但无法弄清楚如何将其与我的产品架构相结合,只能获得某个制造商。

2 个答案:

答案 0 :(得分:1)

使用子查询可能比多个连接更有效:

select model, price 
from (
    select model, price
    from Laptop
    union
    select model, price
    from PC
    union
    select model, price
    from Printer
) t join product p on t.model = p.model
where p.maker = 'B'

答案 1 :(得分:0)

想出来

select Product.model, Laptop.price
from Product, Laptop
where Product.maker = 'B' and Product.model = Laptop.model
union
select Product.model, Printer.price
from Product, Printer
where Product.maker = 'B' and Product.model = Printer.model
union
select Product.model, PC.price
from Product, PC
where Product.maker = 'B' and Product.model = PC.model;