最近了解到,mysql可以直接联表操作:
联表修改:
update t1,t2 set t1.c1=* ,t2.c2=* where t1.c3=t2.c3 and t1.c2=*;
例如:修改纸条状态
update relation , information set relation.status = 2 where relation.infoid=information.infoid and information.group = 0 and relation.id in (125,126,127,128) and relation.owneruid = 1 and relation.type = 1
这样就可以直接修改符合条件的两个表的字段了。
联表删除:
delete t1,t2 from t1 left join t2 on t1.c3=t2.c3 where t1.c2=*;
例如:删除通知:
DELETE relation,
information FROM relation LEFT JOIN information ON relation.infoid = information.infoid WHERE information.group =0 AND relation.id IN ( 19 ,20,21 ) AND relation.owneruid =1
这样就可以一下删除两个表的相关记录了。
另外遍历整个数据表时,有一个比select更快的方法就是handler。大致用法如下:
handler 表名 open;
handler 表名 read next limit n;
handler 表名 close;
例如:遍历delinfoid表
mysql> handler delinfoid open;
mysql> handler delinfoid read next limit 10;
mysql> handler delinfoid read next limit 10;
mysql> handler delinfoid close;
在read时,如果不加limit的话,默认一次只读一条。
联表修改:
update t1,t2 set t1.c1=* ,t2.c2=* where t1.c3=t2.c3 and t1.c2=*;
例如:修改纸条状态
update relation , information set relation.status = 2 where relation.infoid=information.infoid and information.group = 0 and relation.id in (125,126,127,128) and relation.owneruid = 1 and relation.type = 1
这样就可以直接修改符合条件的两个表的字段了。
联表删除:
delete t1,t2 from t1 left join t2 on t1.c3=t2.c3 where t1.c2=*;
例如:删除通知:
DELETE relation,
information FROM relation LEFT JOIN information ON relation.infoid = information.infoid WHERE information.group =0 AND relation.id IN ( 19 ,20,21 ) AND relation.owneruid =1
这样就可以一下删除两个表的相关记录了。
另外遍历整个数据表时,有一个比select更快的方法就是handler。大致用法如下:
handler 表名 open;
handler 表名 read next limit n;
handler 表名 close;
例如:遍历delinfoid表
mysql> handler delinfoid open;
mysql> handler delinfoid read next limit 10;
mysql> handler delinfoid read next limit 10;
mysql> handler delinfoid close;
在read时,如果不加limit的话,默认一次只读一条。