且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何查询从varchar类型和数值中获取最大ID?

更新时间:2023-08-31 19:05:40

看起来值是字符串,它选择了最大字符串.如果要对它们进行数字排序,则必须先将它们转换为数字.您可以使用转换进行此操作:

Looks like the values are strings and it selecting the maximum string. You have to cast them to numbers first if you want them to sort numerically. You can use CONVERT to do this:

SELECT MAX(CONVERT(id, SIGNED)) FROM table

您还可以使用 CAST :

SELECT MAX(CAST(id AS SIGNED)) FROM table

除了CONVERT如果需要它们还有一些其他选择之外,它们几乎做同样的事情.

They do nearly the same thing except CONVERT has some additional options if you need them.