Oracle更新基于另一个表中的值

时间:2011-11-17 17:06:55

标签: oracle oracle-xe

我有两张桌子ROOMS和LEASE,如下所述

create table ROOMS (ROOM_NUM number, FLOOR varchar2(50))
create table LEASE (ROOM_NUM number, RENT_RATE number(4,2))

我想将LEASE表中的rent_rate设置为3.50,用于ROOMS表中FLOOR ='Carpet'的所有房间。

我看过很多例子,但似乎无法提出解决方案。

我意识到我没有制作ROOM_NUM主键或外键,但我只是使用3条记录来测试问题,并确保两个表中都存在房间号

3 个答案:

答案 0 :(得分:1)

UPDATE LEASE
SET RENT_RATE = 3.5
WHERE ROOM_NUM IN (SELECT ROOM_NUM FROM ROOMS WHERE FLOOR='Carpet')

答案 1 :(得分:1)

update lease
   set rent_rate = 3.50
where room_num in(select room_num from rooms where FLOOR='Carpet');

希望有所帮助。

答案 2 :(得分:0)

并且,如果您需要创建行而不是更新现有行:

INSERT INTO LEASE
(SELECT ROOM_NUM, 3.5 FROM ROOMS WHERE FLOOR = 'Carpet');