如何在sql中找到最大值和相关字段?

问题描述 投票:0回答:1

我试图使用以下代码从表中选择最高ID及其描述。但是它一直返回null。请有人建议我哪里出错了

Select  ID, Description
FROM STOPS
where Description = (select MAX(ID) 
from STOPS);
sql
1个回答
3
投票

使用order by和相当于fetch first 1 row only

select s.*
from stops s
order by s.id desc
fetch first 1 row only;

这是ANSI标准语法。并非所有数据库都支持fetch first,有些使用limitselect toprownum

© www.soinside.com 2019 - 2024. All rights reserved.