通过自加入进行更新

发布于 2024-08-24 00:52:25 字数 993 浏览 7 评论 0原文

我想更新一个表以指示某些行是其他行的父行,因此我在表中添加了“parentid”列。以下查询找到所有父级:

SELECT ca1.id, ca2.id 
FROM contactassociations ca1
JOIN contactassociations ca2 ON (ca1.contactid = ca2.contactid)
where ca1.entitytable = 'EMPLOYER' AND
ca2.entitytable = 'CLIENT';

但是当我尝试调整该语法来进行更新时,它不起作用:

UPDATE contactassociations ca1
SET    ca1.parentid = ca2.id
JOIN  contactassociations ca2 ON (ca1.contactid = ca2.contactid)
WHERE ca1.entitytable = 'EMPLOYER' AND ca2.entitytable = 'CLIENT';

我得到:

Error starting at line 6 in command:
UPDATE contactassociations ca1
SET    ca1.parentid = ca2.id
JOIN  contactassociations ca2 ON (ca1.contactid = ca2.contactid)
WHERE ca1.entitytable = 'EMPLOYER' AND ca2.entitytable = 'CLIENT'
Error at Command Line:7 Column:28
Error report:
SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 -  "SQL command not properly ended"
*Cause:    
*Action:

请注意,第 7 行第 28 列是“SET”行的末尾。

I want to update a table to indicate that some rows are parents of others, so I added a "parentid" column to the table. The following query finds all the parents:

SELECT ca1.id, ca2.id 
FROM contactassociations ca1
JOIN contactassociations ca2 ON (ca1.contactid = ca2.contactid)
where ca1.entitytable = 'EMPLOYER' AND
ca2.entitytable = 'CLIENT';

but when I try to adapt that syntax to do the update, it doesn't work:

UPDATE contactassociations ca1
SET    ca1.parentid = ca2.id
JOIN  contactassociations ca2 ON (ca1.contactid = ca2.contactid)
WHERE ca1.entitytable = 'EMPLOYER' AND ca2.entitytable = 'CLIENT';

I get:

Error starting at line 6 in command:
UPDATE contactassociations ca1
SET    ca1.parentid = ca2.id
JOIN  contactassociations ca2 ON (ca1.contactid = ca2.contactid)
WHERE ca1.entitytable = 'EMPLOYER' AND ca2.entitytable = 'CLIENT'
Error at Command Line:7 Column:28
Error report:
SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 -  "SQL command not properly ended"
*Cause:    
*Action:

Note that line 7 column 28 is the end of the "SET" line.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

相对绾红妆 2024-08-31 00:52:25

Oracle 不支持 UPDATE 语句中的 JOIN 子句。

使用这个:

MERGE
INTO    contactassociations ca1
USING   contactassociations ca2
ON      (
        ca1.contactid = ca2.contactid
        AND ca1.entitytable = 'EMPLOYER'
        AND  ca2.entitytable = 'CLIENT'
        )
WHEN MATCHED THEN
UPDATE
SET     parentid = ca2.id

Oracle does not support JOIN clause in UPDATE statements.

Use this:

MERGE
INTO    contactassociations ca1
USING   contactassociations ca2
ON      (
        ca1.contactid = ca2.contactid
        AND ca1.entitytable = 'EMPLOYER'
        AND  ca2.entitytable = 'CLIENT'
        )
WHEN MATCHED THEN
UPDATE
SET     parentid = ca2.id
遗失的美好 2024-08-31 00:52:25

我发现以下样式更容易阅读,但是您需要在 UPDATE 关键字后面使用别名,而不是表名称:

UPDATE ca1
SET    ca1.parentid = ca2.id
FROM contactassociations ca1
LEFT JOIN contactassociations ca2 ON (ca1.contactid = ca2.contactid)
WHERE ca1.entitytable = 'EMPLOYER' AND ca2.entitytable = 'CLIENT'

I find the following style simpler to read, but you need to use an alias after the UPDATE key word, not the table name:

UPDATE ca1
SET    ca1.parentid = ca2.id
FROM contactassociations ca1
LEFT JOIN contactassociations ca2 ON (ca1.contactid = ca2.contactid)
WHERE ca1.entitytable = 'EMPLOYER' AND ca2.entitytable = 'CLIENT'
红ご颜醉 2024-08-31 00:52:25
-- Method #1
update emp set MANAGERNAME= mgr.EMPNAME
FROM SelfJoinTable emp , SelfJoinTable mgr where emp.MANAGERID = mgr.EMPID

-- Method #2
update emp 
set  MANAGERNAME= mgr.EMPNAME  
FROM SelfJoinTable emp 
   LEFT OUTER JOIN SelfJoinTable mgr 
   ON emp.MANAGERID = mgr.EMPID

-- Method #3
update emp 
set  MANAGERNAME= mgr.EMPNAME  
FROM SelfJoinTable emp 
   JOIN SelfJoinTable mgr 
   ON emp.MANAGERID = mgr.EMPID

-- Method #4
update emp 
set  MANAGERNAME= mgr.EMPNAME 
 FROM SelfJoinTable emp 
   inner JOIN SelfJoinTable mgr 
   ON emp.MANAGERID = mgr.EMPID
-- Method #1
update emp set MANAGERNAME= mgr.EMPNAME
FROM SelfJoinTable emp , SelfJoinTable mgr where emp.MANAGERID = mgr.EMPID

-- Method #2
update emp 
set  MANAGERNAME= mgr.EMPNAME  
FROM SelfJoinTable emp 
   LEFT OUTER JOIN SelfJoinTable mgr 
   ON emp.MANAGERID = mgr.EMPID

-- Method #3
update emp 
set  MANAGERNAME= mgr.EMPNAME  
FROM SelfJoinTable emp 
   JOIN SelfJoinTable mgr 
   ON emp.MANAGERID = mgr.EMPID

-- Method #4
update emp 
set  MANAGERNAME= mgr.EMPNAME 
 FROM SelfJoinTable emp 
   inner JOIN SelfJoinTable mgr 
   ON emp.MANAGERID = mgr.EMPID
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文