开发者

sql order by? how do you select the last 25 rows added?

开发者 https://www.devze.com 2023-03-09 03:21 出处:网络
i save a new rows .. and it is saved so id, user, message i want to get the last 25 saved but whit this select top 25 usuario, mensaje

i save a new rows .. and it is saved so

id, user, message i want to get the last 25 saved but whit this

select top 25 usuario, mensaje 
from chat  
order by idchat asc

I just get the first 25, 开发者_如何学Pythonhow can i get the last 25 rows?

the table is it

create table chat
(idchat int primary key identity,
usuario varchar(50),
mensaje text);

if i execute

select top 25 usuario, mensaje from chat  order by idchat desc

it return a bad thing.. for example i a saved

'user1','mensaje1'
'user1','mensaje2'
'user1', 'mensaje3'

this returns

'user1','mensaje3'
'user1','mensaje2'
'user1', 'mensaje1'

I dont need it..


Change ASC to DESC:

select top 25 usuario, mensaje from chat  order by idchat desc

Edit: you want the last 25, but after filtering for the last 25, you want them ordered ascending?

SELECT * FROM
    (
    select top 25 
        usuario, 
        mensaje 
    from 
        chat  
    order by 
        idchat desc
    )
ORDER BY
  idchat ASC
0

精彩评论

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