UPDATE table1 SET column1 = (SUM(table2{& table3} WHERE table2_id1 = id1) WHERE id1 = table2_id1

2022-01-09 00:00:00 sql-update sum where mysql multiple-tables

我想根据主要应用于 table2 但包括表 3 中的单个值的总和来更新 table1.

I'd like to update table1 based upon a sum principally applied on table2 but including a single value from table 3.

table2 有一列与 table1 的 id 列是 FKd,总和基于它们的匹配.

table2 has a column that's FKd to table1's id column, and the sum is based upon them matching.

UPDATE table1, table2 
SET table1.column1 = 
(SELECT SUM( (SELECT constant FROM table3) +
          (SELECT table2.sum_number 
           WHERE table2.table2_id1 = table1.id) ) ) 
WHERE table1.id = table2.table2_id1;

这对我不起作用.

非常感谢!

给定错误

#1064 - You have an error in your SQL syntax; check the manual that corresponds 
 to your MySQL server version for the right syntax to use near 
 'WHERE table2.table2_id1 = table1.id) ) ) WHERE table1.id = table2.table2_id1;'

推荐答案

UPDATE table1, table2 
SET table1.column1 = 
(
    SELECT SUM( 
        (SELECT constant FROM table3) +
        (SELECT table2.sum_number *** WHERE table2.table2_id1 = table1.id) 
    ) 
) 
WHERE table1.id = table2.table2_id1;

上面标有星号的区域没有FROM table2,table1".

There is no "FROM table2,table1" in the area marked with astericks above.

相关文章