ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
有些时候在mySql的SQL语句中使用max(),min()获取最大、最小值时,获取到的结果跟预期的结果不一样,这是因为在查找的时候获取字段的字段类型会影响我们获取的结果,比如我现在遇到的这个,就是。我在获取最大值时,按照以往的经验写的sql语句。 select MAX(ord) from table_namewhere state != 2 and branch_code = 1101 1 但是结果并不是我想要的,最后发现是这个ord时候String类型的,当我转成integer类型再去查时,就能得到我预期的结果。 ** 这里有几种方法去转 ** 1、利用“+”运算符,语法“‘字符串’ + 0”; ~~~ select MAX(ord + 0) from table_name where state != 2 and branch_code = 1101 ~~~ 2、利用cast()函数,语法“cast(‘字符串’ as 数值类型)”; 2.1:取整数:SIGNED ~~~ select MAX(CAST(ord as signed)) from table_name where state != 2 and branch_code = 1101 ~~~ 2.2:取无符号整数 : UNSIGNED ~~~ select MAX(CAST(ord as UNSIGNED)) from table_name where state != 2 and branch_code = 1101 ~~~ 2.3:浮点数 : DECIMAL ~~~ select MAX(CAST(ord as DECIMAL(10,2))) from table_name where state != 2 and branch_code = 1101 ~~~ 3、利用convert()函数,语法“convert(‘字符串’, 数值类型)” 3.1:整数:SIGNED ~~~ select MAX(CONVERT(ord,SIGNED)) from table_name where state != 2 and branch_code = 1101 ~~~ 3.2:无符号整数 : UNSIGNED ~~~ select MAX(CONVERT(ord,UNSIGNED)) from table_name where state != 2 and branch_code = 1101 ~~~ 3.3:浮点数 : DECIMAL ~~~ select MAX(CONVERT(ord,DECIMAL(10,2))) from table_name where state != 2 and branch_code = 1101 ~~~