如何更新与同一表的其他记录匹配的表字段。
UPDATE XXXRLES
SET BIZCAT05 = 'S'
WHERE ( SELECT A.* FROM XXXRLES A, XXXRLES B
WHERE
A.BIZFLD = B.BIZFLD
AND A.BIZOBJ = B.BIZOBJ
AND A.BIZRUL = B.BIZRUL
);
我需要用表XXXRLES的'S'更新Field BIZCAT05当且仅当2个记录具有相同的值时,以下3个字段为:
BIZFLD
BIZOBJ
BIZRUL
如何进行此查询?
答案 0 :(得分:2)
在您在评论中提到ROWID
后,我假设您的RDBMS是Oracle。
UPDATE A SET
BIZCAT05 = 'S'
FROM XXXRLES A
JOIN XXXRLES B ON
A.BIZFLD = B.BIZFLD
AND A.BIZOBJ = B.BIZOBJ
AND A.BIZRUL = B.BIZRUL
AND A.ROWID <> B.ROWID
或使用exists
(看起来更清楚,最后对我来说):
UPDATE A SET
BIZCAT05 = 'S'
FROM XXXRLES A
WHERE EXISTS (SELECT 1
FROM XXXRLES B
WHERE A.BIZFLD = B.BIZFLD
AND A.BIZOBJ = B.BIZOBJ
AND A.BIZRUL = B.BIZRUL
AND A.ROWID <> B.ROWID
)
评论后添加:
我不熟悉DB2,但您可以尝试:
UPDATE XXXRLES A SET
BIZCAT05 = 'S'
WHERE EXISTS (SELECT 1
FROM XXXRLES B
WHERE A.BIZFLD = B.BIZFLD
AND A.BIZOBJ = B.BIZOBJ
AND A.BIZRUL = B.BIZRUL
AND A.ROWID <> B.ROWID
)
答案 1 :(得分:2)
您可以使用RID()行标识符内置函数来标识唯一行。 UPDATE查询可以写成如下:
UPDATE XXXRLES A
SET BIZCAT05 = 'S'
WHERE EXISTS (SELECT 1
FROM XXXRLES B
WHERE A.BIZFLD = B.BIZFLD
AND A.BIZOBJ = B.BIZOBJ
AND A.BIZRUL = B.BIZRUL
AND RID(A) <> RID(B)
)
请在此IBM DB2信息中心中找到有关RID()函数的更多信息。 http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=%2Fcom.ibm.db2z10.doc.sqlref%2Fsrc%2Ftpc%2Fdb2z_bif_rid.htm
答案 2 :(得分:1)
不确定,正确理解你,试试这个
UPDATE XXXRLES SET BIZCAT05 = 'S' WHERE XXXRLES.ID in
( SELECT A.ID FROM XXXRLES A, XXXRLES B
WHERE A.BIZFLD = B.BIZFLD AND A.BIZOBJ = B.BIZOBJ AND A.BIZRUL = B.BIZRUL);