2

对于给定的数据库

Product(maker, model, type)
PC(code, model, speed, ram, hd, cd, price)

问题是: 在 RAM 最低的 PC 中找到同时生产 RAM 最低、处理器速度最快的 PC 的打印机制造商。结果集:制造商。

让我们拆分查询!

  • 查找也生产 PC 的打印机制造商

SELECT DISTINCT maker from product Group By maker,type HAVING type IN('Printer','PC')

我认为这是错误的,因为 IN('Printer','PC') 就像 OR not AND

  • 具有最低 RAM 的 PC

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

  • 具有最低 RAM 的 PC 中速度最快的处理器
WHERE
lowestRam.speed=(SELECT MAX(speed) FROM pc WHERE ram=(SELECT MIN(ram) FROM pc))

查询自己!

SELECT DISTINCT maker FROM
(SELECT DISTINCT model,speed FROM pc WHERE ram=(SELECT MIN(ram) FROM pc)) as lowestRam
INNER JOIN product 
ON product.model=lowestRam.model
WHERE
lowestRam.speed=(SELECT max(speed) FROM pc WHERE ram=(SELECT MIN(ram) FROM pc))
Group By maker,type HAVING type IN( 'Printer' ,'PC')

不幸的是,当我在检查站点上提交查询时,它会产生 1 个额外的错误结果:(

问题来自Link。查询验证有 2 个步骤。第二步仅显示用户和正确结果之间的差异:(

4

8 回答 8

5

这应该可以解决您的问题:

select distinct maker from Product
where type = 'Printer' and maker in (select maker
from Product join PC on Product.model = PC.model
where ram = (select min(ram) from PC) and speed = (select max(speed) from PC 
where ram = (select min(ram) from PC)))
于 2013-07-23T11:53:07.120 回答
1

从我所见,您的查询看起来是正确的(尽管有点过于复杂)。如果您可以提供样本数据和“额外的错误结果”,那么追踪问题会更容易。

这是我的简化版查询:

SELECT DISTINCT p1.maker
FROM product p1, pc
WHERE  p1.model = pc.model
   AND pc.speed = (SELECT MAX (speed)
                   FROM pc
                   WHERE ram = (SELECT MIN (ram) FROM pc))
   AND EXISTS (SELECT 1 from product p2 where maker = p1.maker and p2.type = 'Printer')  
于 2013-06-13T14:53:06.757 回答
0

尝试这个:

    SELECT p.maker 
FROM
(
SELECT *,DENSE_RANK() OVER (ORDER BY ram ASC,speed DESC) AS Rnk
FROM Product p
INNER JOIN (SELECT maker
            FROM Product 
            WHERE type IN ('PC','printer')
            GROUP BY maker
            HAVING COUNT(DISTINCT type)=2
           )p1
ON p1.maker = p.maker
INNER JOIN PC  
ON PC.model = p.model
)t
WHERE Rnk=1
于 2013-06-13T14:38:20.513 回答
0
SELECT  DISTINCT maker
  FROM product
 WHERE model IN (
    SELECT p.model
      FROM pc p JOIN printer r 
                  ON (p.ram =(SELECT min(ram) FROM pc) AND  
                      p.speed=(SELECT max(speed) FROM pc WHERE ram = 
                              (SELECT min(ram) FROM pc))
                     )
                 )
于 2013-09-10T05:26:56.613 回答
0
 SELECT DISTINCT maker FROM    
 (SELECT DISTINCT model,speed FROM pc WHERE ram=(SELECT MIN(ram) FROM pc)) as lowestRam
 INNER JOIN product 
       ON product.model=lowestRam.model AND
 lowestRam.speed=(SELECT max(speed) FROM pc WHERE ram=(SELECT MIN(ram) FROM pc))
 Group By maker HAVING maker IN( SELECT DISTINCT maker FROM Product WHERE type='Printer')
于 2013-10-14T21:22:59.037 回答
0

从产品中选择不同的制造商,pc where product.model in (select model from pc where speed in (select max(speed) from pc group by ram) and ram in (select ram from pc where ram in (select min(ram) from pc))) 和 product.maker in (select maker from product where type = 'printer') and Product.model=PC.model

于 2014-04-30T09:54:32.113 回答
0

你能试试这个:

SELECT DISTINCT G.MAKER FROM PC F,PRODUCT G
WHERE 
F.MODEL = G.MODEL
AND F.SPEED=
(
  SELECT MAX(E.SPEED) FROM PC E WHERE E.RAM =
  (
   SELECT MIN(C.RAM) FROM PC C,PRODUCT D WHERE D.MAKER IN 
          (SELECT A.MAKER FROM PRODUCT A,PRINTER B WHERE A.MODEL=B.MODEL)
  )
)
于 2014-07-13T17:15:09.487 回答
0

从产品中选择不同的制造商,PC where product.model=PC.model and product.type='PC' and speed=(select max(PC.speed) from PC where ram=(select min(PC.ram) from PC) )

于 2014-11-09T00:35:53.127 回答