字段1 字段2
a      b
a      c现在我想将字段2中的值改为如下(所有字段1为a的行,改变字段2为:‘字段1_字段2’)字段1 字段2
a      a_b
a      a_c可以通过sql语句直接来实现吗?

解决方案 »

  1.   

    update tt set 字段2=字段1+'_'+字段2
      

  2.   

    谢谢wwwwb  我试过了,不行!
    还有没有其他办法?是不是需要类型转换?
      

  3.   

    update tt set 字段2=CONCAT(字段1,'_',字段2)
      

  4.   

    你是那个版本的?我这里是mysql 5.0,测试正常。你那里出现什么错误?
      

  5.   

    我也是5.0,我贴出来吧错误
    SQL 查询: UPDATE crop SET city = concat( province, '_', city ) WHERE id =45 MySQL 返回: #1146 - Table 'yiwubase.crop' doesn't exist 
      

  6.   


    mysql> create table c_t (
        -> f1 varchar(255) not null,
        -> f2 varchar(255) not null) engine myisam;
    Query OK, 0 rows affected (0.00 sec)mysql> insert into c_t 
        -> values
        -> ('a','b'),('a','c');
    Query OK, 2 rows affected (0.01 sec)
    Records: 2  Duplicates: 0  Warnings: 0mysql> update c_t set f2 = concat(f1,'_',f2);
    Query OK, 2 rows affected (0.00 sec)
    Rows matched: 2  Changed: 2  Warnings: 0mysql> select * from c_t;
    +----+-----+
    | f1 | f2  |
    +----+-----+
    | a  | a_b | 
    | a  | a_c | 
    +----+-----+
    2 rows in set (0.00 sec)mysql>