已知有两表,如表A,表B。
A表:a_id a_name a_price
1 .. 4.5
2 .. 4
3 .. 6
B表:b_id b_name b_price
1 .. 6
2 .. 7
4 .. 88
5 .. 9
请教下。怎样去update B表,当a_id=b_id时,b_price的值被a_price的值替换?即效果如下:
b_id b_name b_price
1 .. 4.5
2 .. 4
4 .. 88
5 .. 9
请教下应该怎么写?
------解决方案--------------------
SELECT B_ID, B_NAME, NVL(A_PRICE, B_PRICE)
FROM A, B
WHERE B.B_ID = A.A_ID(+)
------解决方案--------------------
update b set b.b_price = (select a.a_price from a where a.a_id=b.b_id)
------解决方案--------------------
sorry,没看到你的需求是update, #2++
------解决方案--------------------
-- 加一个 exists 条件,不然找不到记录时,就更新成 NULL 了。
-- PS : update 语句是拷的 2# 这个兄弟的
update b set b.b_price = (select a.a_price from a where a.a_id=b.b_id)
where exists(select * from a where a.a_id=b.b_id)
------解决方案--------------------
检查一下你的 a 表,看看 a_price 是不是 null 值;
------解决方案--------------------
再加个条件过滤下就是了
update b set b.b_price = (select a.a_price from a where a.a_id=b.b_id)
where exists(select * from a where a.a_id=b.b_id and a.a_price is not null)