mysql两表间select/update/delect

来源:互联网 发布:奥普和欧普哪个好 知乎 编辑:程序博客网 时间:2024/06/13 09:34
--创建测试表,插入测试数据create table t1(    col1 varchar(10),    col2 varchar(10));create table t2(    col1 varchar(10),    col3 varchar(10));--查询,效率对比未知select t1.*,t2.* from t1,t2 where t1.col1 = t2.col1;    --先连接再查询select t1.*,t2.* from t1 join t2 on t1.col1 = t2.col1;  --先查询在连接--更新update t1,t2 set t1.col2 = t2.col3 where t1.col1 = t2.col1;update t1 join t2 on t1.col1 = t2.col1 set t1.col2 = t2.col3;--oracle中可行,mysql报错--[Err] 1288 - The target table t of the UPDATE is not updatableupdate (select t1.col2 f2,t2.col3 f3 from t1,t2 where t1.col1=t2.col1) t set t.f2=t.f3;update (select t1.col2 f2,t2.col3 f3 from t1 left join t2 on t1.col1 = t2.col1) t set t.f2=t.f3;--删除delete t1.*,t2.* from t1,t2 where t1.col1 = t2.col1;delete t1.* from t1 join t2 on t1.col1 = t2.col1;