如何找到最多两列

时间:2017-10-23 12:39:04

标签: sql sqlite

我有一个数据库,其中包含一个用于制造商的列,一个用于laptop.speed的列和另一个用于pc.speed的列。我的问题是如何找到哪个制造商拥有最快的电脑。

SELECT distinct product.maker
        FROM product
        LEFT JOIN PC ON PC.model=product.model
        LEFT JOIN laptop ON laptop.model=product.model
        WHERE laptop.speed=(SELECT MAX(laptop.speed) FROM laptop) OR
              PC.speed=(SELECT MAX(PC.speed) FROM PC)

这段代码让我成为最快的电脑制造商和最快笔记本电脑的制造商。

1 个答案:

答案 0 :(得分:0)

WITH _speeds AS
  (SELECT pc.model AS model,
          pc.speed AS speed
   FROM pc pc
   ORDER BY pc.speed DESC
   LIMIT 1
   UNION SELECT l.model AS model,
                l.speed AS speed
   FROM laptop l
   ORDER BY pc.speed DESC
   LIMIT 1)
SELECT p.maker
FROM product p
JOIN _speeds s ON s.model = p.model
ORDER BY s.speed DESC
LIMIT 1