【发布时间】:2011-07-19 20:17:37
【问题描述】:
我有四个表:产品、电脑、笔记本电脑和打印机。
Products(maker, model, type)
PC(code, model, speed, hd, cd, price)
Laptop(code, model, speed, ram, hd, screen, price)
Printer(code, model, color, type price)
我需要找到价格最高的产品型号(PC、笔记本电脑或打印机)。这不适用于 case 语句,因为如果两个型号的价格最高,则两者都需要显示,并且使用 case 将只选择一个然后退出 case 语句。我想使用 UNION 运算符来执行此操作,但我不知道该怎么做。这是我目前所拥有的:
SELECT model FROM
(SELECT model, MAX(price) FROM
(SELECT model, price FROM Pc UNION ALL SELECT model, price FROM Laptop UNION ALL
SELECT model, price FROM Printer)
GROUP BY model)
但这是不正确的语法,我不知道为什么。有什么想法吗?
【问题讨论】:
标签: sql subquery union aggregate-functions multiple-tables