选择表格的最后一行

时间:2022-05-04 22:19:11

In my database table I have 40 rows, I want to select the last row id(primary key) so I used below code but instead of 40 it returns 14 what to do?

在我的数据库表中我有40行,我想选择最后一行id(主键),所以我使用下面的代码而不是40它返回14做什么?

$num = mysql_query("SELECT id FROM result ORDER BY id DESC LIMIT 1");

2 个解决方案

#1


0  

Try this:

SELECT MAX(id) FROM result

#2


0  

Maybe the order by is sorting lexically,
convert the id to int.

也许order by是按词法排序,将id转换为int。

try this:

$num = mysql_query("SELECT id FROM result ORDER BY cast(ID as unsigned) DESC LIMIT 1");

here's fiddle: http://sqlfiddle.com/#!2/1e214/1

这里是小提琴:http://sqlfiddle.com/#!2 / e214 / 1

http://www.acrobatfaq.com/atbref5/index/ObjectsConcepts/Codingconventions/Sorting-lexicalandnumeri.html

#1


0  

Try this:

SELECT MAX(id) FROM result

#2


0  

Maybe the order by is sorting lexically,
convert the id to int.

也许order by是按词法排序,将id转换为int。

try this:

$num = mysql_query("SELECT id FROM result ORDER BY cast(ID as unsigned) DESC LIMIT 1");

here's fiddle: http://sqlfiddle.com/#!2/1e214/1

这里是小提琴:http://sqlfiddle.com/#!2 / e214 / 1

http://www.acrobatfaq.com/atbref5/index/ObjectsConcepts/Codingconventions/Sorting-lexicalandnumeri.html