SQL whole table update with selection from another table based on the first table field
table1: name | rating | stat1 | stat2
table2: name | stat3 it is forum and site integration, so they are not normalizedI need to recount rating for users in table1.
rating = stat1 + stat2 + stat3.
stat3 is in the table2, which 开发者_运维知识库has common name field with table1.
UPDATE table1 SET rating = stat1 + stat2 +
(SELECT stat3 FROM table2 WHERE name = [name_from_table1] )
How can I insert name from table1 to use it in the table2 selection?
Try this:
UPDATE table1 JOIN table2 ON table1.name=table2.name SET table1.rating=table1.stat1+table1.stat2+table2.stat3
加载中,请稍侯......
精彩评论