心灵预告片SQL

时间:2011-05-05 14:30:33

标签: sql sql-server tsql

我最近一直试图回答http://www.sql-ex.ru/learn_exercises.php#answer_ref上发现的问题,因为我是sql的新手,你可以从我之前关于sql的帖子中看到。我对这个问题:

  

练习:23找到至少生产速度不低于750 MHz的电脑和速度不低于750 MHz的笔记本电脑的制造商。结果集:制作者

以下是我的答案,因为它使用或是错误的。任何人都可以通过给我一个搜索链接或我应该使用什么联接来指导我找到一个好的方向。

  SELECT pt.maker    
    FROM product pt, 
         laptop l, 
         pc    
   WHERE (pt.model = pc.model 
     AND pc.speed >=750) 
      OR (pt.model = l.model 
     AND l.speed >=750)
GROUP BY pt.maker

3 个答案:

答案 0 :(得分:4)

假设支持INTERSECT

SELECT  maker
FROM    product p
JOIN    laptop l
ON      l.model = p.model
WHERE   speed >= 750
INTERSECT
SELECT  maker
FROM    product p
JOIN    pc
ON      pc.model = p.model
WHERE   speed >= 750

跨平台:

SELECT  maker
FROM    (
        SELECT  maker, 1 AS source
        FROM    product p
        JOIN    laptop l
        ON      l.model = p.model
        WHERE   speed >= 750
        UNION
        SELECT  maker, 2 AS source
        FROM    product p
        JOIN    pc
        ON      pc.model = p.model
        WHERE   speed >= 750
        ) q
GROUP BY
        maker
HAVING  COUNT(*) = 2

答案 1 :(得分:3)

关闭,如果表和列定义正确(我没有该站点的登录名),它应该是这样的:

SELECT distinct pt_pc.maker
FROM  
  laptop l 
    inner join product pt_l on pt_l.model=l.model, 
  pc
    inner join product pt_pc on pt_pc.model=pc.model
WHERE pc.speed >=750 and l.speed >=750 and pt_l.maker=pt_pc.maker

所以基本上你想要一台速度大于> = 750的笔记本电脑和一台速度大于等于750的笔记本电脑由同一家伙制作。

答案 2 :(得分:0)

SELECT DISTINCT maker 
FROM product 
WHERE   maker IN (SELECT DISTINCT maker 
                  FROM product AS a 
                  INNER JOIN (SELECT DISTINCT model 
                              FROM pc 
                              WHERE speed>=750) AS b  ON a.model=b.model)
        AND maker IN (SELECT DISTINCT maker 
                      FROM product AS a 
                      INNER JOIN (SELECT DISTINCT model 
                                  FROM laptop 
                                  WHERE speed>=750) AS b ON a.model=b.model)