带连接的Oracle Update查询

时间:2014-03-30 05:50:38

标签: oracle oracle11g

有两张桌子。第一个是费用,第二个是 expenses_items

费用表格中有2列为expns_idexpns_job_type

expenses_items 表中有2列为expns_id(费用表中的外键)和group

我需要更新group表格中的expenses_items,并将其值设置为development,其中expns_job_type表格中expenses的值为{ {1}}。

我试过下面的查询。但没有奏效。

new building

我怎么能这样做?

3 个答案:

答案 0 :(得分:1)

您可以在where子句中使用子查询来限制更新请求:

UPDATE expenses_items ei SET
  group = 'dvelopment'
WHERE EXISTS(
  SELECT expns_id FROM expenses e
  WHERE e.expns_id = ei.expns.id
    AND expns_job_type LIKE 'new building'
)

它也可能是拼写错误,但您使用LIKE子句而不使用通配符。如果您需要完全匹配 - 请使用=代替LIKE

答案 1 :(得分:1)

Oracle使用的UPDATE语法与SQL Server支持不同。有关UPDATE和MERGE语句,请参阅Oracle文档:

http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_10007.htm

http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_9016.htm#i2081218

在你的情况下,这应该是

update expenses_items
set expenses_items."group" = 'development'
where exists (select * from expenses
where expenses_items.expns_id = expenses.expns_id 
and expenses.expns_job_type like 'new building'
)

merge into expenses_items using 
(
 select expenses.expns_id
 from expenses 
 where expenses.expns_job_type like 'new building'
) src on (expenses_items.expns_id = src.expns_id)
when matched then 
  update set expenses_items."group" = 'development'

答案 2 :(得分:1)

Oracle不支持"使用join"进行更新句法。您可以改为:

update expenses_items e
set group = 'development'
where exists 
(
 select 1 
 from expenses 
 where expns_id = e.expns_id and expns_job_type like '%new building%'
)