INNER JOIN和where子句的UPDATE表

时间:2017-01-21 10:38:06

标签: mysql

Tables

如上所示得到2张桌子。我想更新' leaverecord.Consumed'来自' approved.Consumed'在哪里,leaverecord.name = approved.name和leaverecord.leavetype = approved.leavetype。通过错误使用'无效使用群组功能

在以下查询中尝试
UPDATE leaverecord r INNER JOIN approved a
ON r.name = a.name
SET r.Consumed = SUM(DATEDIFF(a.todate,a.fromdate))
WHERE r.leavetype = a.leavetype AND
r.name = a.name

1 个答案:

答案 0 :(得分:0)

计算子查询中的消耗量,并使用leaverecord计算INNER JOIN,然后执行下面的UPDATE。不需要WHERE条款。

试试这个:

update leaverecord r
inner join (
    select name, leavetype, sum(datediff(todate,fromdate)) consumed
    from approved
    group by name, leavetype
) a on r.name = a.name
and r.leavetype = a.leavetype
set r.consumed = a.consumed;