繁体   English   中英

SQL-ex.ru执行14

[英]SQL-ex.ru Execise 14

我正在尝试在http://www.sql-ex.ru/上解决练习14。 查询要求:

找出仅生产相同类型模型的制造商,并且这些模型的数量超过1。推论:制造商,类型

数据库架构如下:

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
)

这不是正确的答案。 我在这里想念什么?

试试这个查询...

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)

Claudiu Haidu的答案是正确的,尽管答案更短:

SELECT maker,MIN(type) 
FROM product 
GROUP BY maker 
HAVING COUNT(distinct type) = 1 AND COUNT(model) >  1

尝试在select使用max(type)做到最简单。

select maker, max(type)
from product
group by maker, type
having count(distinct type)=1 and count(model)>1

在没有where情况where工作,使用了max ,但是它是1个结果中的max,您可以使用具有相同效果的min

select PC.Model,PC.speed,PC.hd 
from PC 
inner join Product ON Product.model = pc.model
where (pc.price < 500)

我刚试过,此代码有效:

Select maker, type
from Product
group by maker
having count(distinct type) =1
and count(distinct model) > 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM