MySQL一条更新语句(+;)=

MySQL一条更新语句(+;)=,mysql,sql-update,Mysql,Sql Update,这是一条oracle语句,我无法在mySQL中运行此连接 update dept x set totsal = (select sum(nvl(sal,0)) from emp e,dept d where e.deptno(+) = d.deptno and x.deptno = e.deptno); 输出应该是 Deptno Dname Loc Totsal 10 ACCOUNTING NEW YORK 8750 20 RESEARCH

这是一条oracle语句,我无法在mySQL中运行此连接

update dept x
set totsal = (select sum(nvl(sal,0))
from emp e,dept d
where e.deptno(+) = d.deptno
and x.deptno = e.deptno);
输出应该是

Deptno    Dname      Loc      Totsal

10    ACCOUNTING  NEW YORK    8750
20    RESEARCH     DALLAS    10875
30    SALES     CHICAGO    9400
40    OPERATIONS  BOSTON    0
    update dept x
    INNER JOIN (
    select d.deptno, sum(ifnull(e.sal,0)) totsal
    from dept  d
    left join emp  e  ON  e.deptno = d.deptno
    GROUP BY d.deptno
    ) t on t.deptno = x.deptno
    set x.totsal = t.totsal
在mysql中应该是

Deptno    Dname      Loc      Totsal

10    ACCOUNTING  NEW YORK    8750
20    RESEARCH     DALLAS    10875
30    SALES     CHICAGO    9400
40    OPERATIONS  BOSTON    0
    update dept x
    INNER JOIN (
    select d.deptno, sum(ifnull(e.sal,0)) totsal
    from dept  d
    left join emp  e  ON  e.deptno = d.deptno
    GROUP BY d.deptno
    ) t on t.deptno = x.deptno
    set x.totsal = t.totsal

您试图在MySql中对
(+)
使用过时的联接语法。

相反,您应该将
dept
加入此查询:

select deptno, sum(sal) totsal
from emp
group by deptno
返回每个部门的总工资:

update dept d
left join (
  select deptno, sum(sal) totsal
  from emp
  group by deptno
) e on e.deptno = d.deptno
set d.totsal = coalesce(e.totsal, 0)
但我相信这是比较简单的:

update dept d
set d.totsal = coalesce((select sum(e.sal) from emp e where e.deptno = d.deptno), 0);

非常感谢这解决了问题(打字错误:x.deptno)@EA90 type更正。。如果我的答案是正确的,请(过去15分钟)将其标记为已接受…看这里如何感谢我接受了答案非常感谢,这真的很有帮助