两表更新的sql问题?

[复制链接]
查看11 | 回复2 | 2014-2-19 11:55:14 | 显示全部楼层 |阅读模式
我想把表B中的两行更新到表A中去。表A和表B的关键行都是tagName下面的sql代码执行时提示
“You can't specify target table 'ybjy_detail_bake' for update in FROM clause”
请问该如何写呢?
update tableA set tableA.unit = (SELECT tableB.unitFROM tableB,tableA where tableB.tagName=tableA.tagName and tableB.Id=1)
回复

使用道具 举报

千问 | 2014-2-19 11:55:14 | 显示全部楼层
最初由 luoxuanhua 发布
[B]我想把表B中的两行更新到表A中去。表A和表B的关键行都是tagName下面的sql代码执行时提示
“You can't specify target table 'ybjy_detail_bake' for update in FROM clause”
请问该如何写呢?
update tableA set tableA.unit = (SELECT tableB.unitFROM tableB,tableA where tableB.tagName=tableA.tagName and tableB.Id=1) [/B]

update a set a.col = (select col from b where a.id=b.id) where exists (select * from b where a.id=b.id)

update (select a.col cola ,b.col colb from a,b where a.id=b.id) set cola=colb
要求b表(源表)有主键
回复

使用道具 举报

千问 | 2014-2-19 11:55:14 | 显示全部楼层
如果b表的id字段是pk的话..
使用下面的格式效率以及可控制性都更好一点..
update (
select a.col1 col1_a,b.col1 col1_b
from a,b
where a.id = b.id
)
set col1_a = col1_b;
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行