update table1 set price=(select tb2.price from tb2 where tb2.id=table1.id)
where exists(select tb2.price from tb2 where tb2.id=table1.id);
where exists(select tb2.price from tb2 where tb2.id=table1.id);
调试欢乐多
update t2 set a.price = b.price
from t2 a,t3 b
where a.id = b.id那遇到更复杂的更新情况,例如要N个表关联,也是要这样写吗?而且为什么要加where exists(select tb2.price from tb2 where tb2.id=table1.id);呢?
直接update table1 set price=(select tb2.price from tb2 where tb2.id=table1.id) 有什么问题吗
update t2 set price = b.price
from t2 a,t3 b
where a.id = b.id那遇到更复杂的更新情况,例如要N个表关联,也是要这样写吗?而且为什么要加where exists(select tb2.price from tb2 where tb2.id=table1.id);呢?
直接update table1 set price=(select tb2.price from tb2 where tb2.id=table1.id) 有什么问题吗
where exists(select 1 from tb2 where tb2.id=table1.id);再试试
table1 table2
id price id price
01 1 01 0
02 4 02 1
03 4 03 1
04 6 04 2
就是让table2的代替table1.price
帮帮忙
---------- ----------
01 1
02 4
03 4
04 6SQL> select * from test1;ID PRICE
---------- ----------
01 0
02 1
03 1
04 2SQL> update test set price=(select price from test1 where id=test.id);已更新4行。SQL> select * from test;ID PRICE
---------- ----------
01 0
02 1
03 1
04 2SQL>
例如,我们要取最大的一个,就可以用max(price)