我写了这段代码
select maker.product , product.type
from product
join laptop on laptop.model=product.model
join pc on pc.model=product.model
join printer on printer.model=product.model
WHERE (product.type= 'pc' AND
(NOT EXISTS (SELECT maker
FROM Product
WHERE product.type = 'laptop'
))and (NOT EXISTS (SELECT maker
FROM Product
WHERE product.type = 'printer'
))
or
product.type= 'laptop' AND
(NOT EXISTS (SELECT maker
FROM Product
WHERE product.type= 'pc'
))and (NOT EXISTS (SELECT maker
FROM Product
WHERE product.type= 'printer'
))
or
type = 'printer' AND
(NOT EXISTS (SELECT maker
FROM Product
WHERE product.type= 'laptop'
))and (NOT EXISTS (SELECT maker
FROM Product
WHERE product.type= 'pc'
)))
group by product.maker
having count(product.model) > 1
在练习14中的这个网站sql-ex.ru但是没有用。 问题是: 对于Product表,以表格形式接收结果集,其中包含列:maker,pc,laptop和printer。对于每个制造商,如果制造商具有相应类型的产品,则该表必须包含“是”,否则必须包含“否”。在第一种情况下(是),在括号中指定(不含空格)相应类型的可用不同模型的数量(即在PC,笔记本电脑和打印机表中)。
它的方案是
抱歉我说英语不好! 请帮忙 ! 谢谢。
答案 0 :(得分:0)
这是一个更易读的代码版本(除非我犯了错误):
select maker.product , product.type
from product
join laptop on laptop.model=product.model
join pc on pc.model=product.model
join printer on printer.model=product.model
WHERE (
product.type= 'pc' AND
(NOT EXISTS (SELECT maker FROM Product WHERE product.type = 'laptop')) and
(NOT EXISTS (SELECT maker FROM Product WHERE product.type = 'printer'))
or
product.type= 'laptop' AND
(NOT EXISTS (SELECT maker FROM Product WHERE product.type= 'pc')) and
(NOT EXISTS (SELECT maker FROM Product WHERE product.type= 'printer'))
or
type = 'printer' AND
(NOT EXISTS (SELECT maker FROM Product WHERE product.type= 'laptop')) and
(NOT EXISTS (SELECT maker FROM Product WHERE product.type= 'pc'))
)
group by product.maker
having count(product.model) > 1
基本上,看起来你需要在每个OR之间做括号。