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

时间:2022-12-13 17:07:23

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

我想基于主要应用于table2但是包含表3中的单个值的总和来更新table1。

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

table2有一个列为FKd到table1的id列,并且总和基于它们的匹配。

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;

That doesn't work for me.

这对我不起作用。

Many thanks in advance!

提前谢谢了!

EDIT: Error Given

编辑:错误给定

#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;'

3 个解决方案

#1


3  

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;

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

上面标有星号的区域没有“FROM table2,table1”。

#2


0  

Try this:

尝试这个:

UPDATE 
    table1 t1
INNER JOIN
    table2 t2 ON t2.table2_id1 = t1.id
SET
    t1.column1 = (SELECT constant FROM table3) + t2.sum_number

#3


0  

try this:

尝试这个:

Update table1 t1 join table2 t2 on t1.id = t2.table2_id1
SET t1.column1 = (SELECT constant FROM table3) + t2.sum_number

#1


3  

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;

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

上面标有星号的区域没有“FROM table2,table1”。

#2


0  

Try this:

尝试这个:

UPDATE 
    table1 t1
INNER JOIN
    table2 t2 ON t2.table2_id1 = t1.id
SET
    t1.column1 = (SELECT constant FROM table3) + t2.sum_number

#3


0  

try this:

尝试这个:

Update table1 t1 join table2 t2 on t1.id = t2.table2_id1
SET t1.column1 = (SELECT constant FROM table3) + t2.sum_number