MySQL Database Implementation association table update SQL statement

Source: Internet
Author: User
Tags mysql update one table

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


Table 2

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.