有两个表A,B, A 里面 有 id 和 Count 两个字段,id 主键自动加一
              B 中有 key,a_id 两个字段,key 主键自动加一A 中的Count 记录的是B里,a_id=A.id 的个数。现在有一些数据需要插入到B中,是插入一个就更新一下A中的Count好,还是全部插入完成后 用SQL语句统计后再更新好?当两个表中的数据量变大后,用哪一种方法效率更高一点?跪求高手指点,若能讲解一二,则不甚感激!

解决方案 »

  1.   

    这种关系比较密切,功能单一的情况下,使用触发器更新A表里数据即可。
    你只管向B表里写数据,不用考虑A表。
      

  2.   

    我用的是 Mysql 5.1
      

  3.   


    请问 Mysql 一般用什么GUI工具来写触发器?还是直接用SQL语句?
      

  4.   

    create trigger ins_count BEFORE INSERT ON B FOR EACH ROW 
     begin
       update A SET @A.Count=@Count+1 where A.id=a_id
     end; ERROR 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@[email protected]+1 where A.id=a_id' at line 3???
      

  5.   


    create trigger ins_count AFTER INSERT ON B FOR EACH ROW 
    begin
      update A SET @Count = @Count + 1 where id=new.a_id;
    endERROR 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@[email protected]+1 where A.id=a_id' at line 3