我正在尝试更新大约800行,并希望更新table_c,它从table_A和table_b匹配两个值。 Table_a和table_c通过value_id相关联。
示例:
CREATE TABLE TABLE_A (VALUE_ID INTEGER, PERSON_ID INTEGER, LAST_NAME VARCHAR2(15), FIRST_NAME VARCHAR2(15));
CREATE TABLE TABLE_B (VALUE_ID INTEGER, LAST_NAME VARCHAR2(15), FIRST_NAME VARCHAR2(15), ALIAS_ID INTEGER);
CREATE TABLE TABLE_C (VALUE_ID INTEGER, ALIAS_ID INTEGER, PERSON_ID INTEGER);
-----
INSERT INTO TABLE_A (VALUE_ID, PERSON_ID, LAST_NAME, FIRST_NAME)
VALUES (1, 6069, 'SMITH', 'JOHN');
INSERT INTO TABLE_A (VALUE_ID, PERSON_ID, LAST_NAME, FIRST_NAME)
VALUES (2, 6111, 'ADAMS', 'JOHN');
INSERT INTO TABLE_A (VALUE_ID, PERSON_ID, LAST_NAME, FIRST_NAME)
VALUES (3, 6117, 'ADAMS', 'SAM');
----
INSERT INTO TABLE_B (VALUE_ID, LAST_NAME, FIRST_NAME, ALIAS_ID)
VALUES (4, 'SMITH', 'JOHN', 40856);
INSERT INTO TABLE_B (VALUE_ID, LAST_NAME, FIRST_NAME, ALIAS_ID)
VALUES (5, 'ADAMS', 'JOHN', 3425);
INSERT INTO TABLE_B (VALUE_ID, LAST_NAME, FIRST_NAME, ALIAS_ID)
VALUES (6, 'ADAMS', 'SAM', 40831);
-----
INSERT INTO TABLE_C (VALUE_ID, ALIAS_ID, PERSON_ID)
VALUES (7, 28, 6069);
INSERT INTO TABLE_C (VALUE_ID, ALIAS_ID, PERSON_ID)
VALUES (8, 1022, 6111);
INSERT INTO TABLE_C (VALUE_ID, ALIAS_ID, PERSON_ID)
VALUES (9, 40473, 6117)
我尝试使用update语句来完成此操作和/或更新语句,但它不起作用。不确定我是否需要IF / ELSE声明。
最后,表B和表C中的alias_id必须相同。所以,这就是为什么table_C是将要更新的那个。
这是我到目前为止所拥有的
UPDATE TABLE_C
SET C.ALIAS_ID = (SELECT B.ALIAS_ID
FROM TABLE_B B
JOIN TABLE_A A
ON A.FIRST_NAME = B.FIRST_NAME AND A.LAST_NAME = B.LAST_NAME);
答案 0 :(得分:0)
您可以使用以下使用UPDATE ... FROM
SQL Server语法的查询:
UPDATE TABLE_C
SET ALIAS_ID = b.ALIAS_ID
FROM TABLE_C AS c
INNER JOIN TABLE_A AS a ON c.PERSON_ID = a.PERSON_ID
INNER JOIN TABLE_B AS b ON a.FIRST_NAME = b.FIRST_NAME
AND a.LAST_NAME = b.LAST_NAME
这可能适用于Oracle:
MERGE
INTO TABLE_C
USING (
SELECT c.PERSON_ID AS pid, b.ALIAS_ID AS bAlias
FROM TABLE_C AS c
JOIN TABLE_A AS a ON c.PERSON_ID = a.PERSON_ID
JOIN TABLE_B AS b ON a.FIRST_NAME = b.FIRST_NAME
AND a.LAST_NAME = b.LAST_NAME
)
ON (PERSON_ID = pid)
WHEN MATCHED THEN
UPDATE
SET ALIAS_ID = bAlias;