MySql: Compare 2 strings which are numbers?

前端 未结 4 1145
失恋的感觉
失恋的感觉 2021-01-18 03:40

I have a column in my table, it holds values such as 100012345. The column is varchar. Now I want to compare this to similiar values in a where:

... where my         


        
4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-18 04:10

    Have you tried to do it normally, like this:

    ... where myColumn > 100012345
    

    That should work!, mysql automatically casts a string to number when it's in the context of a number operation. In the same way it casts a number to string if it's used in a string context. See the examples in the type conversion docs:

    To cast a string to a numeric value in numeric context, you normally do not have to do anything other than to use the string value as though it were a number:

    mysql> SELECT 1+'1';
           -> 2
    

    If you use a string in an arithmetic operation, it is converted to a floating-point number during expression evaluation.

    If you use a number in string context, the number automatically is converted to a string:

    mysql> SELECT CONCAT('hello you ',2);
            -> 'hello you 2'
    

提交回复
热议问题