简体   繁体   English

选择查询到表的最后一行在mysql中给出错误的结果

[英]Select query to last row of table giving wrong result in mysql

I want to fetch last row of the table. 我想获取表的最后一行。 For this i have used to below query but it returns me 99 where my table contains approx 123 and last pro_id = 123. Right result should be 123. 为此,我习惯于下面的查询,但它返回我99,其中我的表包含大约123,最后一个pro_id =123。正确的结果应该是123。

please suggest me for right way : 请以正确的方式建议我:

 SELECT * FROM product ORDER BY pro_id DESC LIMIT 1

I think the problem is that your pro_id is character and it will be ordered by alphabetical order. 我认为问题在于您的pro_id是字符,将按字母顺序排序。 You could try to convert it to number first. 您可以尝试先将其转换为数字。

SELECT * FROM product ORDER BY CAST(pro_id AS UNSIGNED) DESC LIMIT 1

That's most likely because your pro_id column is defined as text. 这很可能是因为您的pro_id列定义为文本。 You can tell sql to order by the numeric value in that column with 您可以告诉sql按该列中的数字排序

SELECT * FROM product ORDER BY CONVERT(pro_id, UNSIGNED INTEGER) DESC LIMIT 1

This query will display a true desc result: 此查询将显示真实的desc结果:

select pro_id from
(SELECT cast(replace(pro_id,' ','') as UNSIGNED) as pro_id from product) as a
ORDER BY pro_id desc limit 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM