MySQL 选择sql表的最后3行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18420126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select last 3 rows of sql table
提问by Amir Sadegh
I want to select the last 3 rows of an sql table. I know I should use SELECT * FROM table ORDER BY DESC LIMIT 3, but the problem with this code is that it selects the rows from the end. For example, it selects 30, then 29, then 28. But, I need them in this format: 28, 29, 30. Any suggestion?
我想选择 sql 表的最后 3 行。我知道我应该使用SELECT * FROM table ORDER BY DESC LIMIT 3,但是这段代码的问题在于它从最后选择行。例如,它选择 30,然后是 29,然后是 28。但是,我需要它们的格式如下:28, 29, 30. 有什么建议吗?
回答by null.point3r
Try this:
尝试这个:
SELECT * FROM (
SELECT * FROM reset ORDER BY id DESC LIMIT 3
) as r ORDER BY id
回答by errorintheapplication
I hope this help your problem
我希望这可以帮助您解决问题
select * from
(
select * from reset
order by id DESC LIMIT 3
) t
order by id ASC
回答by Rahul Tripathi
Try something like this:-
尝试这样的事情:-
SELECT * FROM reset
WHERE username = '$table' ORDER BY id ASC LIMIT (FOUND_ROWS() - 3), 3
回答by Colin 't Hart
How about something like:
怎么样:
select * from (select * from table order by x desc limit 3) order by x;
回答by Yajur
try
尝试
Select * from (SELECT * FROM Table_name ORDER BY Column_name DESC limit 0,3) as alias ORDER BY Column_name ASC;
回答by Hasan ZainAlDeen
try this manual one !
试试这本手册!
easy and Simple !!
简单易行!!
Select * From tableName where
PKCol=(select count(*) from tableName )
OR
PKCol=(select count(*) from tableName )-1
OR
PKCol=(select count(*) from tableName )-2
order by PKCol desc;

