For example, there are two tables, one table, the value of a field to be associated with another table for statistics, you need to use the MySQL Update method, and a left join another table for federated query.
Table 1
The requirements are: The ID in tab 1, the number of times the tagid appears in Table 2, is updated to the Videonum field in table 1. Two tables need to be associated to update table 1 and count.
MySQL Association table Update STATISTICS
The SQL statement is as follows:
The code is as follows |
Copy Code |
UPDATE V9_keyword as a left JOIN V9_keyword_data as B On A.id=b.tagid Set a.videonum= (SELECT COUNT (tagid) from V9_keyword_data where tagid=a.id) WHERE A.id=b.tagid; |
Note that the order and writing of Update,left Join,on,set,where, in addition to the statistics here A.videonum used to select subqueries, why to use this, please read this article: MySQL execution count error.
This article has two key points:
The SQL statement of the update of the 1.mysql Update Association table
2. Correlation table Statistics The correct usage of a field count