简体   繁体   English

使用联接SQL Oracle更新表

[英]Update table using join SQL Oracle

I need to update several columns of a table, but one piece of information is stored in another table, so I'm assuming I need to join them but I'm unsure how to. 我需要更新一个表的几列,但是一条信息存储在另一张表中,所以我假设我需要将它们连接起来,但是不确定如何进行。 Basically I need to update the job title where the office is T06 and the start date where the original start_date was '05-FEB-09, 08:00' for the staff whose last name is Parker 基本上,我需要更新姓氏为Parker的工作人员的职务,其中办公室为T06,起始日期为原始start_date为'05 -FEB-09,08:00'

So far I have: 到目前为止,我有:

UPDATE JOB
SET JOB_TITLE = 'Head of Technology'
WHERE OFFICE = 'T06' 
AND SET START_DATE = '26-JUN-17, 08:00'
WHERE START_DATE = '05-FEB-09, 08:00'
FROM JOB
JOIN STAFF
ON JOB.STAFF_ID = STAFF.STAFF_ID
WHERE STAFF.LAST_NAME = 'Parker';

Correct syntax would be 正确的语法是

update job
set job_title = 'Head of Technology',
    start_date = '26-JUN-17, 08:00'
where office = 'T06'
      and start_date = '05-FEB-09, 08:00'
      and staff_id =(select staff_id
                     from staff
                     where last_name = 'Parker'
                    );

However, it might need to be modified ( START_DATE condition is strange). 但是,可能需要对其进行修改( START_DATE条件很奇怪)。

Also, it is unclear what START_DATE datatype is. 另外,还不清楚START_DATE数据类型是什么。 Should be DATE , but your code suggests a string ( VARCHAR2 ). 应该DATE ,但是您的代码建议使用字符串( VARCHAR2 )。


Alternative #1 (note TO_DATE ) function: 备选方案1(请注意TO_DATE )功能:

update job
set job_title = 'Head of Technology',
    start_date = to_date('26-JUN-17, 08:00', 'dd-mon-yy, hh24:mi')
where office = 'T06'
      and start_date = to_date('05-FEB-09, 08:00', 'dd-mon-yy, hh24:mi')
      and staff_id =(select staff_id
                     from staff
                     where last_name = 'Parker'
                    );

Alternative #2 (note CASE and absence of START_DATE in WHERE clause): 备选方案2(请注意,在WHERE子句中为CASE并且缺少START_DATE ):

update job
set job_title = 'Head of Technology',
    start_date = case when start_date = to_date('05-FEB-09, 08:00', 'dd-mon-yy, hh24:mi')
                           then         to_date('26-JUN-17, 08:00', 'dd-mon-yy, hh24:mi')
                      else start_date
                 end
where office = 'T06'
      and staff_id =(select staff_id
                     from staff
                     where last_name = 'Parker'
                    );

I would use EXISTS : 我会使用EXISTS

UPDATE JOB
    SET JOB_TITLE = 'Head of Technology'
        OFFICE = 'T06',
        START_DATE = '26-JUN-17, 08:00'
WHERE START_DATE = '05-FEB-09, 08:00' AND
      OFFICE = 'T06' AND
      EXISTS (SELECT 1
              FROM STAFF
              WHERE JOB.STAFF_ID = STAFF.STAFF_ID AND
                    STAFF.LAST_NAME = 'Parker' 
             );

The most native way is to use updatable join view 最原生的方法是使用可更新的联接视图

Simple make you join and select all relevant columns, than add UPDATE modifying the join. 简单使您可以联接并选择所有相关列,而不是添加UPDATE来修改联接。

update (
 select job.*
 FROM JOB
 JOIN STAFF
 ON JOB.STAFF_ID = STAFF.STAFF_ID
 WHERE STAFF.LAST_NAME = 'Parker')
set JOB_TITLE = 'Head of Technology',
START_DATE = '26-JUN-17, 08:00'

The only requirement is that the column STAFF_ID in STAFF table is backed with a unique index (eg primary key). 唯一的要求是在STAFF表中的STAFF_ID列上要支持唯一索引(例如主键)。

Without this you get the ERROR ORA-01779: cannot modify a column which maps to a non key-preserved table ORA-01779: cannot modify a column which maps to a non key-preserved table您将收到错误ORA-01779: cannot modify a column which maps to a non key-preserved table

This way of UPDATE is very usefull if you need to update with a value from the joined table (not your case). 如果您需要使用联接表中的值(而不是您的情况)进行更新,则这种UPDATE方法非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM