How can I select latest row from by table without sorting it?
It is because it follow by the ID AUTO INCREMENT...
I'm using c# asp.net to select... I did try using LIMIT 5 but it give me an error page..
rSQL = "select COUNT(*) from chatLog_db where sessionid='" + grpID + "' LIMIT 5";
Is there any better way to solve this matter?
I'd appreciate any help please.
You have an id column which is autoincremented, right? Then you can do it like this..
select * from tablename where id=(select MAX(rid) from tablename)
You can try
SELECT * FROM chatLog_db WHERE sessionid > (SELECT MAX(sessionid) - 1 FROM chatLog_db);
You may also try for
SELECT * FROM chatLog_db WHERE sessionid > (SELECT MAX(sessionid) - 5 FROM chatLog_db);
You may use max
as well like
select * from chatLog_db where sessionid = (select max(sessionid) from chatLog_db);
Something like that.
If you are not using order by
into your query because you are thinking that it will change the order of your dsplay data then i will tell you that there is one trick as well to sort
your data as per your need
you can also sort your data as per your need even if you are using order by into your query,put the result into DataView and sort it according to your need because DataView allow us sorting facility as well.
Latest by using Order By
like
select * from tablename order by columnname desc LIMIT 5;
Hope it works for you.
if the latest means the max id
select * from chatLog_db
where id = (select max(id) from chatLog_db);
EDIT
select 5 records
select * from chatLog_db
where id > (select max(id) - 5 from chatLog_db);
On MSSQL simply use the top 1
instead of limit
select top(1) * from mytable order by some_column
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.