[英]Error with sql max function: Incorrect syntax near the keyword 'select'
我有下一個sql代碼,您可以在下面看到。 它可以正常工作,但條件所在的最后一行除外。
select m.model,m.price from (
select product.model, pc.price
from product
join pc
on product.model=pc.model
union
select product.model, laptop.price
from product
join laptop
on product.model=laptop.model
union
select product.model, printer.price
from product
join printer
on product.model=printer.model
) m
where m.price = select max(m.price) from m
我遇到下一個錯誤:關鍵字“選擇”附近的語法不正確。 如果我把最后一行where m.price = m.price
: where m.price = m.price
它也可以工作
您是否追求最高的價格?
select m.model,MAX(m.price) as price from
(
select product.model, pc.price
from product
join pc
on product.model=pc.model
union
select product.model, laptop.price
from product
join laptop
on product.model=laptop.model
union
select product.model, printer.price
from product
join printer
on product.model=printer.model
) m
GROUP BY m.model
如果您正在尋找價格最高的型號:
select m.model, m.price
from (select p.model, pc.price
from product p join
pc
on p.model = pc.model
union all
select p.model, laptop.price
from product join
laptop
on p.model = laptop.model
union all
select p.model, printer.price
from product p join
printer
on p.model = printer.model
) m
order by m.price desc
limit 1;
但是,這是非常復雜的方法,因為不需要join
:
select m.model, m.price
from (select pc.model, pc.price
from pc
union all
select laptop.model, laptop.price
from laptop
union all
select printer.model, printer.price
from printer
) m
order by m.price desc
limit 1;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.