我的SQL查询中的子查询错误

时间:2016-03-17 23:16:29

标签: sql subquery logic

https://screenshot.googleplex.com/j4igYFL9sjH 发现打印机制造商还生产具有最低RAM容量的PC和具有最低RAM容量的所有PC的最高处理器速度。 结果集:制造商。

我的查询:

SELECT maker FROM Product WHERE type ='Printer' AND model IN(SELECT DISTINCT(model), hd, ram FROM PC 
                        WHERE hd = (
                                SELECT MAX(hd) FROM PC 
                                     WHERE model IN(SELECT DISTINCT(model) FROM PC
                                                                       WHERE ram <=(SELECT MIN(ram) FROM PC))));

事实证明,这里没有制造者名单。我认为子查询有问题。

5 个答案:

答案 0 :(得分:4)

我真的不喜欢这个问题,因为它的措辞含糊不清。希望这可以帮助别人解决我刚刚弄清楚要问的问题:

SELECT DISTINCT maker FROM Product WHERE type = 'printer' AND maker IN 
  ( SELECT maker FROM Product WHERE model IN ( 
       SELECT model FROM Pc 
          WHERE ram = (SELECT MIN(ram) FROM PC)   
          AND speed = (SELECT MAX( speed) FROM 
             (SELECT speed FROM Pc WHERE 
                 ram = (SELECT MIN(ram) FROM Pc)) as z4) 
             )
  )

答案 1 :(得分:0)

<强>问题

  1. 无法保证具有给定类别的PC maker也会产生打印机...当前查询不会预期,因此它可能会返回

  2. 当前的子查询不足以解决问题,整个查询不起作用,因为子查询返回具有最小RAM,最大处理器,最大硬盘等的PC的model,然后该模型与model中的所有打印机product进行了比较..因此它永远不会找到任何匹配项。

  3. <强>解决方案

    1. 翻转查询,以便主查询成为子查询,反之亦然,因此子查询是关于获取所有打印机maker
    2. 一旦翻转,让PC打印机maker处于任何状态都很容易,只需使用order by ..
    3. 守则

      此代码将返回打印机maker,该打印机也会生成具有最大hd和最小ram

      的PC
      SELECT maker FROM product A INNER JOIN PC B ON A.model = B.model 
      WHERE maker IN (SELECT DISTINCT maker FROM product WHERE type = 'printer')
      ORDER BY hd DESC, ram ASC
      LIMIT 1
      

答案 2 :(得分:0)

这是我的答案,希望对您有所帮助

SELECT distinct maker FROM product WHERE type = 'printer'
AND maker IN (SELECT maker
FROM product JOIN ( 
SELECT model, speed, ram FROM pc WHERE speed =
                (SELECT MAX(speed) FROM pc
                WHERE ram = (SELECT MIN(ram) FROM pc)
                ) 
            AND ram = (SELECT MIN(ram) FROM pc)
            ) b
ON product.model = b.model)

使用:

SELECT model, speed, ram FROM pc WHERE speed = 
(SELECT MAX(speed) FROM pc 
WHERE ram = (SELECT MIN(ram) FROM pc)) 

你确定它会以100的速度返回任何东西。 假设你有1,2,3型号,他们的速度都是100,但他们的公羊不同,它将返回所有三个模型,而真正只有模型1具有最低的公羊。所以你要添加

AND ram = (SELECT MIN(ram) FROM pc)

作为附加条件,以确保在使用pc标准模型时,ram的要求不会消失。

答案 3 :(得分:0)

select distinct maker
from product
where 
 model in (
select model 
from  (select * from pc where ram in (select min (ram) from pc)) as A
where speed in 

(select max(speed) from
          (select * from pc  where ram in (select min(ram) from pc ))      as B)
)
and maker in (select maker  from product where type like 'printer')

答案 4 :(得分:0)

我采用了分而治之的方法

WITH pc_lowest_ram as (
  SELECT DISTINCT model, pc.ram FROM pc
  WHERE pc.ram = (
   SELECT MIN(ram) FROM PC
  )
),
printer_makers AS (
  SELECT DISTINCT maker FROM product p
  WHERE p.type = 'Printer'
),
pc_max_speed AS (
  SELECT MAX(speed) pm FROM pc
  JOIN pc_lowest_ram plr on (plr.model = pc.model)
  WHERE plr.ram = pc.ram
),
pc_models AS (
  SELECT pc.model pm FROM pc
  JOIN pc_lowest_ram plr on (plr.model = pc.model)
  WHERE plr.ram = pc.ram
  AND pc.speed = (
    SELECT pm FROM pc_max_speed
  )
)
SELECT DISTINCT p.maker FROM product p
JOIN printer_makers ON (printer_makers.maker = p.maker)
JOIN pc_models ON (pc_models.pm = p.model)