开发者

Selecting a row with the greater serial number

开发者 https://www.devze.com 2023-03-02 14:02 出处:网络
so I have a sql table which includes rows with serial numbers. Now I need to select only one row, and it should be the one with the largest serial number.

so I have a sql table which includes rows with serial numbers. Now I need to select only one row, and it should be the one with the largest serial number.

Ex

17 Cool Dude
18 Riha

I need to select the row "18 Riha" ....how 开发者_如何学Godo i do that?

Thanks.


SELECT * FROM table ORDER BY serial DESC LIMIT 1;


In MSSQL you can use the TOP keyword.

SELECT TOP 1 * FROM table ORDER BY serial DESC


EDIT : Oracle-Specific

Get the maximum serial number and then get the row/rows (if the max serial number is not unique) that has/have the serial number.

select * from table1
where id = (select max(id) from table1);

Or order the results by ID descending and then get the first row in the result set..

select * from (
select * from table1
order by id desc)
where rownum = 1

or Use analytic functions...

select * from 
(select t1.*, rank () over (order by t1.id desc) rnk
  from table1 t1
) where rnk = 1;
0

精彩评论

暂无评论...
验证码 换一张
取 消