sql语句将字符串转换为数字默认去掉单引号中的空格,遇到空格作为字符串截止,
结果为123
MySQL里面如何用sql语句让字符串的‘123’转换为数字的123?
方法一:SELECT CAST('123' AS SIGNED integer);
方法二:SELECT CONVERT('123',SIGNED);
方法三:SELECT '123'+0;
同样,当需要将一列字符串转换为数字也可以使用此方法,或者需要用到字符串进行排序的时候可以使用到此方法:
- SELECT * FROM answer WHERE question_id = xx ORDER BY CONVERT(`code`,SIGNED)
- SELECT * FROM answer WHERE question_id = xx ORDER BY cast(`code` as signed integer)