Update XXX set XXX where 这种写法大家肯定都知道,update和delete支持inner join的update方式,这个在表间关联来做更新和删除操作非常有用.
列子:
- update tb_User
- set pass=''
- from tb_User usr
- inner join tb_Address addr on usr.nAddressFK = addr.nAddressID
- where usr.id=123
update的格式是
update t1 set t1.name=’Liu’ from t1 inner join t2 on t1.id = t2.tid
MYSQL,ACCESS 写法如下:
- UPDATE mem_world AS mw1 INNER JOIN mem_world AS mw2
- ON mw1.parentid = mw2.wid
- SET mw1.level = mw2.level
- WHERE mw2.baseid = 107
- AND mw2.parentid = 0
- AND mw2.size > 1;
on是表连接的筛选条件
就是说,表连接后,会产生一个类似于临时的视图这么一个东西
where是从这个临时的视图中筛选数据的
所以,你首先要搞清,你的所谓的2个条件属于哪一种
Delete
delete 语句也是类似
delete from t1 from t1 inner join t2 on t1.id = t2.tid
注意蓝色部分。
mysql:
- DELETE mwb FROM mem_world_building AS mwb INNER JOIN mem_world AS mw
- ON mwb.wid = mw.wid
- where mw.type between 11 and 15
- and baseid = 107
- and mw.parentid <> 0
- and mw.size > 1;
下面是ORACLE的:
- DELETE TABLE1 where exists ( select 1 from table2 where and table1.khid=table2.khid and FWDWID=8);
- DELETE TABLE1 where KHID exists ( select KHID from table2 where FWDWID=8)
总结:
sql server下应该这么写
delete from a from a inner join b on a.id=b.id
而Access中写为
delete from a inner join b on a.id=b.id
update 语句
Access
update a inner join b on a.id=b.id set a.pwd=b.pwd
SQL Server
update a set a.pwd=b.pwd from a inner join b on a.id=b.id
关于SQL中逻辑查询处理的各个阶段 参考http://blog.csdn.net/feixianxxx/archive/2009/10/18/4694354.aspx