当数据库字段为varchar
要用数字大小排序时:
select * from table order by cast(filed as signed) ;
当作为筛选条件时
:必须加单引号
字符串和数字做比较的话,是将字符串转换成数字
对索引字段做函数操作,可能会破坏索引值的有序性,因此优化器就决定放弃走树搜索功能
SELECT * FROM table where filed = 1;
等价于:SELECT * FROM table where CAST(filed AS signed int) = 1;
参考文献:
https://blog.csdn.net/u010825931/article/details/103811797