数据库架构
问题 -
找出所有产品(任何类型)的型号和价格 由制造商B制作。
我的尝试 -
SELECT b.model, b.price FROM laptop AS b
WHERE b.model IN ( SELECT model FROM product WHERE maker = 'B')
UNION
SELECT c.model, c.price FROM pc AS c
WHERE c.model IN ( SELECT model FROM product WHERE maker = 'B')
UNION
SELECT d.model, d.price FROM printer AS d
WHERE d.model IN ( SELECT model FROM product WHERE maker = 'B')
即使这没有显示只有B的正确数据集 -
SELECT b.model, b.price FROM laptop AS b
WHERE b.model IN ( SELECT model FROM product WHERE maker = 'B')
只有一个结果集即将到来。
让我知道使用查询进行的逻辑错误。
编辑 -
输出 -
SELECT model FROM product WHERE maker = 'B'
SELECT b.model, b.price FROM laptop AS b
WHERE b.model IN ( SELECT model FROM product WHERE maker = 'B')
查询 - SELECT * FROM product WHERE maker = 'B'
输出 -
答案 0 :(得分:1)
SELECT b.model, b.price FROM laptop AS b
...
SELECT c.model, c.price FROM laptop AS c
...
SELECT d.model, d.price FROM laptop AS d
您从笔记本电脑中选择3次,结果相同。然后你UNION
将它们放在一起,这将删除所有重复项。
编辑:您的示例查询;
SELECT b.model, b.price FROM laptop AS b
WHERE b.model IN ( SELECT model FROM product WHERE maker = 'B')
只会找到笔记本电脑,因为这是唯一使用过的表格。 1121是PC,因此查询不会显示它。