如何查询从varchar类型和数值中获取最大ID? [英] how to query get max id from varchar type and the values in numeric?

查看:112
本文介绍了如何查询从varchar类型和数值中获取最大ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

i有表ID和列ID,其值为1、2、3,...,10、11、12、13. 如何查询从varchar类型获取最大id?我尝试过

i have table and column id, and the values are 1, 2 ,3,...,10,11,12,13. how to query get max id from varchar type ? i had try

select MAX(id) from table

但结果为9,请帮助??

but the result is 9, Please help ??

推荐答案

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

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.

这篇关于如何查询从varchar类型和数值中获取最大ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆