MySQL连表更新和删除的差别

1、连表更新和删除

可以使用其他表(或一个查询视图)更新、删除本表,单要注意表连接更新和表连接删除时使用方式的差距

[sql]  view plain copy
  1. CREATE TABLE `test1` (  
  2.   `id` int(11) NOT NULL,  
  3.   `namevarchar(16) DEFAULT NULL,  
  4.   PRIMARY KEY (`id`)  
  5. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  
  6.   
  7. create table test2 like test1;  
  8.   
  9.   
  10. -- 根据test2更新test1的name列,条件是id相同  
  11. -- 表连接  
  12. update test1 a inner JOIN test2 b on a.id=b.id set a.name=b.name ;  
  13.   
  14. update test1 a , test2 b set a.name=b.name where a.id=b.id ;  
  15.   
  16.   
  17. --  删除test1表中id和test2表中id重复的  
  18. --  表连接  
  19. delete a from test1 a inner JOIN test2 b on a.id=b.id ;  
  20. --  子查询  
  21. delete from test1 where exists (select 1 from test2 where test1.id=test2.id);  
  22. delete a from test1 a where exists (select 1 from test2 where test1.id=test2.id); 


2、like性能

cola like 'mysql%'  为了提高速度,可以改成   cola>='mysql'  and cola<mysqm


3、其他

insert delete 操作是不允许使用表别名的。
update where条件中不允许自连接
update test set id=1 where pid in(select id from test where pid=12); 是不允许的,可以使用临时表解决。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值