简体   繁体   English

使用来自另一个表的数据更新一个表的记录

[英]Update records of one table using data from another table

I've two tables. 我有两张桌子。

Table 1: Employees 表1:员工

EID Name    Gender
1   Peter   M
2   John    M
3   Melissa F

Table 2: Salary 表2:薪金

EID Salary
1   6000
2   8000
3   10000

I need to raise salary of male employees by 10% and Female employees by 15%. 我需要将男性雇员的工资提高10%,将女性雇员的工资提高15%。

Below is the query that I've used but can't achieve required result in Oracle11g. 以下是我使用过但无法在Oracle11g中达到所需结果的查询。

merge into salary
using employees on 
salary.eid = employees.eid
when matched then
update set
    salary.salary = 1.1*salary where employee.gender = 'M' ,
    salary.salary = 1.15*salary where employee.gender = 'F';

I got below error message: 我收到以下错误消息:

SQL Error: ORA-00969: missing ON keyword 00969. 00000 - "missing ON keyword" *Cause: SQL错误:ORA-00969:缺少ON关键字00969。00000-“缺少ON关键字” *原因:
*Action: *行动:

There are two things which you need to consider in the snippet provided. 所提供的代码段中需要考虑两件事。

  1. ON clause should always be accompanied by "()". ON子句应始终带有“()”。
  2. WHERE clause in UPDATE statement is not correct. UPDATE语句中的WHERE子句不正确。 Hope this snippet helps. 希望此代码段对您有所帮助。

     MERGE INTO SALARY USING EMPLOYEES ON (salary.eid = employees.eid) WHEN MATCHED THEN UPDATE SET salary.salary = DECODE(employee.gender,'M',1.1*salary,'F',1.15*salary) ; 

Try this 尝试这个

Update salary 
set salary.salary=salary.salary*(select case t.gender when 'M' then 1.1 When 'F' then 1.15 end from employees t where t.eid= salary.eid)

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

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