在表的更新操做中,在不少狀況下須要在表達式中引用要更新的表之外的數據。象sql server提供了update的from 子句,能夠將要更新的表與其它的數據源鏈接起來。雖然只能對一個表進行更新,可是經過將要更新的表與其它的數據源鏈接起來,就能夠在update的表達式中引用要更新的表之外的其它數據。
例如:sql
UPDATE Table2
SET Table2.ColB = Table2.ColB + Table1.ColB
FROM Table2
INNER JOIN Table1
ON (Table2.ColA = Table1.ColA);server
實際更新的操做是在要更新的表上進行的,而不是在from子句所造成的新的結果集上進行的。源碼
Oracle沒有update from語法,能夠經過兩種寫法實現一樣的功能:
1:子查詢UPDATE A SET A.NAME=(SELECT B.NAME FROM B WHERE B.ID=A.ID),本查詢要根據具體狀況看看是否變通成以下
(1)單列
UPDATE Aqt
SET A.NAME=(SELECT B.NAME FROM B WHERE B.ID=A.ID)io
WHERE A.ID IN (SELECT ID FROM B);table
(2)多列
UPDATE order_rollupdate
SET(qty,price)=(SELECT SUM(qty),SUM(price) FROM order_lines WHERE customer_id='KOHL' )select
WHERE cust_id='KOHL' AND order_period=TO_DATE('01-Oct-2000')rollup
源碼語法
update "SHOP_GOODS_DATE_copy" copy set(HOTEL_INFO_NAME,
TITLE,
UP_DOWN_STATUS,HOTEL_ID)=(
SELECT
HOTEL_INFO_NAME,
SGHB.TITLE,
UP_DOWN_STATUS,
SGHB.HOTEL_ID
FROM SHOP_GOODS_HOTEL_INFO SGHI
LEFT JOIN(
SELECT
TITLE,
HOTEL_BODY_ID,
HOTEL_ID
FROM SHOP_GOODS_HOTEL_B
)SGHB ON SGHI.HOTEL_BODY_ID=SGHB.HOTEL_BODY_ID
WHERE SGHI.HOTEL_INFO_ID=copy.HOTEL_INFO_ID
)
2:利用視圖來作
UPDATE (SELECT A.NAME ANAME,B.NAME BNAME FROM A,B WHERE A.ID=B.ID)
SET ANAME=BNAME;
例如:
UPDATE tablea a
SET a.fieldforupdate = (SELECT b.fieldsource FROM tableb b WHERE a.keyfield = b.keyfield)
WHERE EXISTS (SELECT b.fieldsource FROM tableb b WHERE a.keyfield = b.keyfield)
select * from (
select HOTEL_INFO_NAME,TITLE,"DATE", DATAS from
( select D.HOTEL_INFO_NAME,D.TITLE,D."DATE", D.INFO_PRICE||','||D.TOTEL_NUMBER||','||D.GOODS_NUMBER DATAS from
( SELECT DATE_ID,
"DATE",
GOODS_NUMBER,
INFO_PRICE,
CTIME,
UTIME,
HOTEL_INFO_ID,
TOTEL_NUMBER,
HOTEL_INFO_NAME,
TITLE,
UP_DOWN_STATUS,
HOTEL_ID
FROM "SHOP_GOODS_DATE_copy"
where HOTEL_ID='UWFNJ1K2O3160UTCIEQK'
AND "DATE">='2018-04-24'
AND "DATE"<='2018-04-30'
order by "DATE" )D
)) pivot (max(DATAS) for "DATE" in ('2018-04-24' date1, '2018-04-25' date2,'2018-04-26' date3, '2018-04-27' date4, '2018-04-28' date5, '2018-04-29' date6, '2018-04-30' date7));
有三點須要注意: