mysql - 返回单行子查询的更新语句返回多行

标签 mysql sql oracle

我是 SQL 新手,但我被查询困住了。

我有 3 个表员工、部门和工资。 我正在尝试通过给出此条件来更新工资表中的奖金列

give 10% bonus on total salary to the employees who are not in IT departments. 

我想出了这个查询

update salary_paid 
set bonus=(select (0.1*total_salary) "Bonus" 
           from salary_paid, departments, employees
           where 
               employees.department_id=departments.department_id and 
               employees.employee_id=salary_paid.employee_id and
               departments.department_name!='IT')
           ;

但是它返回此错误

ORA-01427: single-row subquery returns more than one row

我对此一无所知,请帮忙。 提前致谢

最佳答案

您的内部查询(select (0.1*total_salary) "Bonus"from salary_paid 返回多个值,因此无法分配给奖金列。

尝试使用这样的连接进行更新

   UPDATE 
    (SELECT salary_paid.bonus as oldBonus, 0.1*salary_paid.total_salary as newBounus
     FROM salary_paid
     INNER JOIN employees
     ON salary_paid.employee_id = employees.employee_id
     INNER JOIN departments
     ON departments.department_id = employees.department_id 
     WHERE departments.department_name != 'IT'
    ) t
    SET t.oldBonus= t.newBounus

关于mysql - 返回单行子查询的更新语句返回多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22602787/

相关文章:

mysql - 当 Mysql 中的 select 查询中使用 GROUP BY 子句时,增量值不是按顺序排列的

sql - 这个SQL语句有什么问题

mysql - 为什么 MySQL 游标在报告成功时停止循环

sql - 多列子查询更新... SQL

sql - 如何用MATCH(), ROUND() & Subquery写MySQL全文查询?

sql - Oracle错误 "inconsistent datatypes: expected CHAR got LONG"

MySQL - 如何求和时间?

sql - 在 sql 中按 IS NOT NULL AND 过滤

mysql - 在任何记录字段上进行 SQL 文本搜索

oracle - ROracle SELECT 语句中的绑定(bind)参数