最近项目有个需求,mysql一条update语句实现批量更新数据的东西表结构如下
olduser表
------------------------
 username | userid
  李明        196 
  张三        145 
-----------------------
authors表
-----------------------
 author  | authorid
  李明        null
  张三        null
-----------------------update authors set authorid=
(select olduser.userid from olduser,authors where authors.author=olduser.username)  //以中文名实现关联
where author=(select username from olduser,authors where authors.author=olduser.username ) //错误的行问题:想这么写的,但是where条件返回的是多个值,可定不能一一对应,望高手赐教

解决方案 »

  1.   


    UPDATE AUTHORS
       SET AUTHORID = (SELECT OLDUSER.USERID
                         FROM OLDUSER, AUTHORS
                        WHERE AUTHORS.AUTHOR = OLDUSER.USERNAME LIMIT 1)
     WHERE AUTHOR = (SELECT USERNAME
                       FROM OLDUSER, AUTHORS
                      WHERE AUTHORS.AUTHOR = OLDUSER.USERNAME LIMIT 1)
      

  2.   

    我运行之后,不知道为什么 
    You can't specify target table 'authors' for update in FROM clause
      

  3.   

    那你就再复制一个authors的表,select 中用复制的表代替老的authors
      

  4.   

    update authors set authorid = 
    (select olduser.userid from olduser where authors.author=olduser.username)
    where exists (select username from olduser where authors.author=olduser.username )
      

  5.   

    1楼的方法,我试过了。where条件limit 1 只更新限制一条,不能全部更新lemon520的where条件用了exists 一条也更新不了 结果---Query OK, 0 rows affected (46.625 sec)个人写了个存储过程解决了,但是希望可以研究一下update多表批量更新............以下是建表语句,大家试试一起研究一下   o(∩_∩)o CREATE TABLE `authors` (
     `author` varchar(15),
     `authorid` mediumint(8),
      PRIMARY KEY  (`author`)
     ) 
    CREATE TABLE `olduser` (
     `username varchar(15),
     `uerid` mediumint(8),
      PRIMARY KEY  (`username`)
     ) INSERT INTO `authors` (`author`, `authorid`) VALUES 
      ('张三', 1),
      ('李四', 2),
      ('王五', 3);
    INSERT INTO `olduser` (`username`, `userid`) VALUES 
      ('张三', 0),
      ('李四', 0),
      ('王五', 0);
      

  6.   

    补充:1楼的根本就更新不了,逻辑不对.既然限制了当然不可以全部更新.无法满足交集的条件当然报如下错了You can't specify target table 'AUTHORS' for update in FROM clause期待高手补充中..........
      

  7.   

    UPDATE AUTHORS
       SET AUTHORID = (SELECT OLDUSER.USERID
                         FROM OLDUSER, AUTHORS
                        WHERE AUTHORS.AUTHOR = OLDUSER.USERNAME)
      

  8.   

    TRY IT ..
    UPDATE AUTHORS A1
       SET A1.AUTHORID = (SELECT U1.USERID
                            FROM OLDUSER U1
                           WHERE A1.AUTHOR = U1.USERNAME)
     WHERE EXISTS (
                   SELECT 1
                     FROM OLDUSER U1
                    WHERE A1.AUTHOR = U1.USERNAME
                  ); 
                  
      

  9.   

    update authors set authorid =
    (select olduser.userid from olduser where authors.author=olduser.username) 这样就可以
      

  10.   

    create table newuser as select * from olduser o,authors a where a.author=o.username
    update newuser set uerid=authorid
    alter table newuser drop author
    alter table newuser drop authorid