数据库
首页 > 数据库> > mysql-UPDATE table1 SET column1 =(SUM(table2 {&table3} WHERE table2_id1 = id1)WHERE id1 = table2_id1

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

作者:互联网

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

table2的一列与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;

那对我不起作用.

提前谢谢了!

编辑:给出错误

#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”.

标签:multiple-tables,mysql,sum,sql-update,where
来源: https://codeday.me/bug/20191013/1905495.html