此查询中的错误在哪里? (sql-ex.ru exercise 25)

时间:2018-02-19 17:42:30

标签: mysql sql

我正在尝试从sql-ex.ru解析Exercise 25

  

数据库方案由四个表组成:

     
      
  • 产品(制造商,型号,型号)
  •   
  • PC(代码,型号,速度,内存,高清,CD,价格)
  •   
  • 笔记本电脑(代码,型号,速度,内存,高清,屏幕,价格)
  •   
  • 打印机(代码,型号,颜色,类型,价格)
  •   
     

发现打印机制造商还生产具有最低RAM容量且具有最低RAM容量的所有PC的最高处理器速度的PC。   结果集:制造商。

我看不出为什么以下查询不会在第二个检查数据库上产生所需的结果:

SELECT DISTINCT product.maker
FROM product
RIGHT JOIN printer ON printer.model = product.model
WHERE product.maker IN(
    SELECT product.maker
    FROM product
    RIGHT JOIN pc ON pc.model = product.model
    WHERE pc.ram = (SELECT MIN(ram) FROM pc)
    AND pc.speed = (
        SELECT MAX(t.speed)
        FROM (SELECT speed FROM pc WHERE ram = (SELECT MIN(ram) FROM pc)) AS t
    )
)

错误如下:

Wrong
Your query produced correct result set on main database, but it failed test on second, checking database
* Wrong number of records (less by 1)

为什么它不能完全选择1个制造商?

3 个答案:

答案 0 :(得分:0)

我不知道查询中的错误在哪里,但您可以使用以下内容:

WITH t AS ( SELECT * FROM PC c WHERE c.ram = (SELECT MIN(ram) FROM PC) ) SELECT DISTINCT a.maker FROM Product a WHERE a.type = 'PC' AND a.maker IN ( SELECT b.maker FROM Product b WHERE b.type = 'Printer' ) AND a.model IN ( SELECT model FROM t WHERE speed = (SELECT MAX(speed) FROM t) )

答案 1 :(得分:0)

这是解决此问题的方法:

WITH CTE_Product AS
(
    SELECT DISTINCT
        Maker
    FROM
        Product
    WHERE
        Type = 'PC'
    AND
        Maker IN (SELECT Maker FROM Product WHERE Type = 'Printer')
)

select c.maker from CTE_Product C
inner join
(
select distinct maker from product inner join PC on product.model=pc.model 
where type='PC'and  
pc.speed in 
          (select max(speed) 
           from pc 
           where ram=
                     (select min(ram)from pc)
           )
 and pc.ram=
             (select min(ram)
              from pc)
) A on A.maker = C.maker

答案 2 :(得分:0)

SELECT DISTINCT maker
  FROM Product
 WHERE TYPE = 'Printer'

 INTERSECT

SELECT DISTINCT maker
  FROM Product AS p
    INNER JOIN PC as pc
    ON p.model = pc.model
    WHERE
      type = 'PC'
      AND (pc.ram = ALL (SELECT MIN(ram) FROM PC))
        AND (pc.speed = (SELECT MAX(speed) FROM PC WHERE ram = (SELECT MIN(ram) FROM PC )))