原字段类型为var, 有部分数据为数值(如"100"),部分数据为字符("不限"),变更类型到数值型,如INT,是不是那些字符都变成0,数值的那些结果不变,只是类型变了?谢谢!

解决方案 »

  1.   

    [email protected]_monitor>select * from test;
    +----+-------+
    | id | value |
    +----+-------+
    |  1 | 1     | 
    |  2 | 2     | 
    |  3 | 3     | 
    |  4 | 5     | 
    |  5 | 6     | 
    |  6 | a     | 
    |  7 | b     | 
    |  8 | abc   | 
    +----+-------+
    8 rows in set (0.00 sec)[email protected]_monitor>alter table test modify value int;
    Query OK, 8 rows affected, 3 warnings (0.03 sec)
    Records: 8  Duplicates: 0  Warnings: [email protected]_monitor>select * from test;
    +----+-------+
    | id | value |
    +----+-------+
    |  1 |     1 | 
    |  2 |     2 | 
    |  3 |     3 | 
    |  4 |     5 | 
    |  5 |     6 | 
    |  6 |     0 | 
    |  7 |     0 | 
    |  8 |     0 | 
    +----+-------+
    8 rows in set (0.00 sec)当更改表内的数据列类型时,mysql会自动调用convert或者cast,我测试了一下
    [email protected]_monitor>select convert('a',UNSIGNED);   
    +-----------------------+
    | convert('a',UNSIGNED) |
    +-----------------------+
    |                     0 | 
    +-----------------------+
    1 row in set, 1 warning (0.00 sec)
    mysql会直接将非数字转化成0