第一張表ROOMDB:
| 房間號 | 租金余額 |
|---|---|
| N327 | 0 |
第二個表RENTALINVOICE:
| 發票日期 | 房間號 | 總計 |
|---|---|---|
| 11/26/2021 | N327 | 2,200.00 |
我的更新代碼:
UPDATE ROOMDB
SET
RENTALBALANCE = (SELECT TOTALDUE
FROM RENTALINVOICE
WHERE RENTALINVOICE.ROOMNUMBER=ROOMDB.ROOMNUMBER
AND INVOICEDATE=SYSDATE) ;
我需要用資料更新totaldue列中ROOMDB的資料,RENTALINVOICE盡管它成功地將 2,200 輸入到totaldue列中,同時它也清除了ROOMDB.
每次我更新它時,它都會洗掉除roomnumber 我指定的記錄之外的其余記錄。請幫忙。
uj5u.com熱心網友回復:
您基本上似乎還會更新表roomdb中沒有行的表中的行rentalinvoice,因此該列rentalbalance將設定為空。
看看下面的例子:
drop table a;
create table a (id number, cost number);
insert into a values (1, 1);
insert into a values (2, 2);
drop table b;
create table b (id number, cost number);
insert into b values (1, 100);
-- updates all rows in a and sets cost to null whenen there is no row in b
update a set cost = (select cost from b where a.id = b.id);
select * from a;
-- only updaes rows in a where there is a row in b
update a set cost = id;
update a set cost = (select cost from b where a.id = b.id) where exists (select 1 from b where a.id = b.id);
select * from a;
uj5u.com熱心網友回復:
當您執行這樣的更新命令時,Oracle 將更新所有表行。對于每一行,選擇命令將使用這些行值執行。如果更新中select的結果沒有找到任何記錄將回傳null。在這種情況下,該RENTALBALANCE列也將設定為 null。
(SELECT TOTALDUE
FROM RENTALINVOICE
where WHERE RENTALINVOICE.ROOMNUMBER = ROOMDB.ROOMNUMBER
AND INVOICEDATE = SYSDATE)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/367858.html
