我正在努力解决练习14. http://www.sql-ex.ru/。查询要求:
找出只生产相同型号的制造商的制造商 这些模型的数量超过1. Deduce:maker,type
数据库架构如下:
Product(maker, model, type)
PC(code, model, speed, ram, hd, cd, price)
Laptop(code, model, speed, ram, hd, screen, price)
Printer(code, model, color, type, price)
我写了以下查询;
select distinct maker, type
from product
where maker in (
select product.maker
from product, ( select model, code
from printer
union
select model, code
from pc
union
select model, code
from laptop
) as T
where product.model = T.model
group by product.maker
having count(T.code) > 1 and count(distinct product.type) = 1
)
这不是正确的答案。我在这里缺少什么?
答案 0 :(得分:5)
尝试此查询...
SELECT DISTINCT maker, type
FROM product
WHERE maker IN
(SELECT DISTINCT maker
FROM product
GROUP BY maker HAVING COUNT(distinct type) = 1
AND count(model) > 1)
答案 1 :(得分:2)
Claudiu Haidu的答案是正确的,尽管答案更短:
SELECT maker,MIN(type)
FROM product
GROUP BY maker
HAVING COUNT(distinct type) = 1 AND COUNT(model) > 1
答案 2 :(得分:0)
尽量使用max(type)
中的select
尽可能做到最好。
select maker, max(type)
from product
group by maker, type
having count(distinct type)=1 and count(model)>1
在没有where
的情况下工作,使用max
,但最多只有1个结果,你可以使用相同效果的min
答案 3 :(得分:0)
select PC.Model,PC.speed,PC.hd
from PC
inner join Product ON Product.model = pc.model
where (pc.price < 500)
答案 4 :(得分:0)
我刚试过,这段代码有效:
Select maker, type
from Product
group by maker
having count(distinct type) =1
and count(distinct model) > 1