本帖最后由 netxuning 于 2010-01-08 15:37:40 编辑

解决方案 »

  1.   

    update a set num = select count(*) from b where rid=a.rid;
      

  2.   

    update a inner join
    (select rid,count(*) as toa from b group by rid) b
    on a.rid=b.rid
    set a.num=b.toa
      

  3.   

    update a set num = (select count(*) from b where rid=a.rid);
      

  4.   

    我记着mysql的update是不支持楼上这种写法的可以这么些
    update a ,(select count(*) num,rid from b group by rid) b set a.num=b.num WHERE b.rid=a.rid
      

  5.   

    update a ,(select count(*) as cnt  from b group by rid) x
    set a.num=x.cnt
    where a.rid=b.rid
      

  6.   

    3楼的应该不行。where rid=a.rid出现了a.rid
      

  7.   


    mysql> select * from a;
    +------+------+
    | rid  | num  |
    +------+------+
    |    1 | NULL |
    |    2 | NULL |
    |    3 | NULL |
    +------+------+
    3 rows in set (0.02 sec)mysql> select * from b;
    +------+------+
    | id   | rid  |
    +------+------+
    |    1 |    1 |
    |    2 |    1 |
    |    3 |    1 |
    |    4 |    2 |
    |    5 |    3 |
    |    6 |    3 |
    +------+------+
    6 rows in set (0.00 sec)mysql> update a set num=(select count(*) from b where rid=a.rid);
    Query OK, 3 rows affected (0.05 sec)
    Rows matched: 3  Changed: 3  Warnings: 0mysql> select * from a;
    +------+------+
    | rid  | num  |
    +------+------+
    |    1 |    3 |
    |    2 |    1 |
    |    3 |    2 |
    +------+------+
    3 rows in set (0.00 sec)mysql> select version();
    +------------------+
    | version()        |
    +------------------+
    | 5.1.36-community |
    +------------------+
    1 row in set (0.00 sec)
      

  8.   

    果然可以!
    zhoupuyue(.)  的方法可行。