[英]Insert id in sql table based on another particular value in another table
[英]SQL Updating a particular rows based on the ID in another table
我有兩張桌子
Price
RET_ID DBS_ID RRP
Database
DBS_ID PRO_ID
我想使用UPDATE,以便如果Database表中的PRO_ID = 1,則可以將Price表中的RRP提高20%
UPDATE
( SELECT RRP
FROM PRICE
JOIN database
ON database.dbs_id = price.dbs_id
)
SET rrp = 100
WHERE (database.pro_ID = 1);
我一直在嘗試各種方式的INNER JOIN ... ON,但無濟於事。 謝謝。
您可以將過濾條件放在where
子句中。 這是一個使用exists
的方法:
update price p
set rpr = rpr * 1.2
where exists (select 1
from database d
where d.dbs_id = p.dbs_id and d.pro_id = 1
);
這是in
另一種使用方式:
update price p
set rpr = rpr * 1.2
where p.dbs_id in (select d.dbs_id
from database d
where d.pro_id = 1
);
UPDATE Price P SET RRP = RRP*1.2
WHERE EXISTS
(select 1 from Database
where DBS_ID = P.DBS_ID AND PRO_ID = 1);
您可以使用此查詢
MERGE INTO PRICE P
USING DATABASE D ON
(P.DBS_ID=D.DBS_ID) AND
D.PRO_ID=1
WHEN MATCHED
THEN
UPDATE SET
P.RRP=P.RRP*1.2;
從另一個表更新表時,請始終使用MERGE函數,這在SQL中是一個很棒的概念
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.