简体   繁体   English

如何从其他表列的SUM结果更新列?

[英]How to Update a column from the results of a SUM from other table column?

What im looking for is to update a column from table 1 using the results from the SUM of a different column in table 2 for example : 我要寻找的是使用表2中不同列的SUM结果更新表1中的列:

table 1 表格1

id             | views | 
--------------------
1          |   0  |  
2          |   0  |  

table 2 表2

show_id    | views | 
--------------------
1          |   5   |  
1          |   10  |  
1          |   10  |  
2          |   10  |  
2          |   10  |  
2          |   10  |  

Now the two tables are connected by id and show_id there the same value; 现在,两个表通过id和show_id连接在一起,具有相同的值; what i want is to add the sum of views from show_id to id where show_id=id 我想要的是将show_id的视图总和添加到id,其中show_id = id

so at the end table 1 should look like these : 因此,在最终表1中应如下所示:

id         | views | 
--------------------
1          |   25  |  
2          |   30  |  

I'm using MySQL 5.1.30 我正在使用MySQL 5.1.30

Use an update with join : join使用update

UPDATE table1 t1
INNER JOIN table2 t2 ON (t1.id = t2.show_id)
SET t1.views = SUM(t2.views)
GROUP BY t1.id;

See: http://dev.mysql.com/doc/refman/5.0/en/update.html 参见: http : //dev.mysql.com/doc/refman/5.0/en/update.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM