使用另一个 table 的数据更新一个 table 的记录
Update records of one table using data from another table
我有两张桌子。
Table 1: 员工
EID Name Gender
1 Peter M
2 John M
3 Melissa F
Table2:工资
EID Salary
1 6000
2 8000
3 10000
我需要将男性员工的工资提高 10%,女性员工的工资提高 15%。
以下是我使用过但在 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';
我收到以下错误消息:
SQL Error: ORA-00969: missing ON keyword
00969. 00000 - "missing ON keyword"
*Cause:
*Action:
您需要在提供的代码段中考虑两件事。
- ON 子句应始终伴随“()”。
UPDATE 语句中的 WHERE 子句不正确。希望这段代码对您有所帮助。
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) ;
试试这个
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)
我有两张桌子。
Table 1: 员工
EID Name Gender
1 Peter M
2 John M
3 Melissa F
Table2:工资
EID Salary
1 6000
2 8000
3 10000
我需要将男性员工的工资提高 10%,女性员工的工资提高 15%。
以下是我使用过但在 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';
我收到以下错误消息:
SQL Error: ORA-00969: missing ON keyword 00969. 00000 - "missing ON keyword" *Cause:
*Action:
您需要在提供的代码段中考虑两件事。
- ON 子句应始终伴随“()”。
UPDATE 语句中的 WHERE 子句不正确。希望这段代码对您有所帮助。
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) ;
试试这个
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)