*编辑:我正在使用sqlite3
我有一个包含3个表的数据库;电脑,笔记本和打印机。除了其他之外,他们还有列模型和价格。我试图从任何表格中选择最高价格的模型。我的尝试引导我:
select model
from
(select model, max(price)
from
(select model, price
from laptop
union all
select model, price
from printer
union all
select model, price
from pc));
这给了我所有表中的最后一个模型。但是,如果我这样做:
select model, p
from
(select model, max(price) as p
from
(select model, price
from laptop
union all
select model, price
from printer
union all
select model, price
from pc));
它给了我正确的结果,但我也坚持了价格。我不明白这是怎么可能的。
答案 0 :(得分:0)
这应该做你想做的事情,并且还会带回关系:
select x.model
from (select model, price
from laptop
union all
select model, price
from printer
union all
select model, price
from pc) x
join (select max(max_price) as max_price
from (select max(price) as max_price
from laptop
union all
select max(price)
from printer
union all
select max(price)
from pc)) y
on x.price = y.max_price