JIYIK CN >

Current Location:Home > Learning > DATABASE > MySQL >

Update multiple tables in MySQL with one query

Author:JIYIK Last Updated:2025/04/24 Views:

In some cases, users want to update logically related tables at the same time. These logically related tables are linked to each other through some attributes.

Advantages of updating multiple tables in one MySQL query

Similar attributes in the tables are used to create update queries. Update queries perform updates of multiple rows in different tables based on conditions.

When similar situations occur, multiple table update queries are suitable. The benefits of using this type of multi-table update query are:

UPDATEUpdate multiple tables with one query in MySQL using keyword

In a multi-table update query, every record that meets the criteria is updated. Even if the criteria match multiple times, the row is updated only once.

The syntax for updating multiple tables cannot be used with the ORDER BYand LIMITkeywords.

UPDATEThe syntax of the keyword:

UPDATE table1, table2, ...
    SET column1 = value1,
        column2 = value2,
        ...
[WHERE conditions]

MULTIPLE UPDATE is not a composite keyword present in MySQL language. The syntax is formed by the combination of various keywords that help in grouping two or more tables, such as joinkeyword.

Join type is used to make a single query to update multiple tables at once. Let us understand this by updating multiple tables at once.

Consider two tables named libraryand stu_book. And consider the case where a book is sent from a library to a student.

The number of books in the library decreases, while the number of books with students increases. This is the scenario that requires two separate calls.

To avoid separate updates in RDBMS tables, we use a single query to update the rows in both the tables. Following is the list of statements to be executed before the update call.

List of queries before the actual query on the table:

Query and execute MySQL statement:

UPDATE library l, stu_book s
    SET l.book_count = l.book_count - 2,
        s.book_count = s.book_count + 2
WHERE l.id = s.book_id;

In the above query, internally, inner join merges two tables and operates on the merged table after checking the constraints of the tables. When no keyword is specified, inner join is applied.

For joins like outer join, right outer join, user should use right keyword. Join can be performed only if the two tables being grouped have similar/matching attributes.

SETThe keyword UPDATEis used along with the keyword to set new values ​​in an existing row. It overwrites the old values ​​by writing the new data over them.

Here set updates stu_bookthe book quantity of the table and the same amount libraryis decremented from the book quantity.

The counts for all rows of both tables are not updated. Instead, the limit WHEREis preserved by the keyword.

WHEREKeywords do the actual filtering of the rows. Keywords filter the rows after checking the conditions in the table.

Here, this keyword indicates library_idthat should match stu_bookthe table's book_id.

The output will list the results of the total number of rows affected in both tables. Below is the output confirming the number of rows affected.

Query OK, 2 rows affected (0.02 sec)
Rows matched: 2  Changed: 2  Warnings: 0

The following is the final output of the above query in the local running environment or command prompt.

Local running screenshots:

Trigger updated output

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Get the version in MySQL

Publish Date:2025/04/24 Views:169 Category:MySQL

In this article, we will learn how to get the current version of MySQL on Windows systems. It is necessary for businesses to maintain versions of different tools and software used to run their business in order to keep systems compatible. T

Full Join in MySQL

Publish Date:2025/04/24 Views:70 Category:MySQL

This article aims to explore how to perform a full join or full outer join in MySQL. Full outer join is used to merge or combine the entire data from two separate tables. For example, suppose we have two tables named student_id and student_

Grouping by month in MySQL

Publish Date:2025/04/24 Views:166 Category:MySQL

In this article, we will learn how to group values ​​by month in MySQL database. Businesses and organizations have to find user or customer data based on purchase or usage trends over a few months. If a particular business achieves its

Enabling the slow query log in MySQL

Publish Date:2025/04/24 Views:177 Category:MySQL

Today, we will enable MySQL in MySQL using MySQL shell on Windows and Ubuntu 20.04 slow_query_log . For this tutorial, we are using MySQL version 8.0 and Ubuntu 20.04. MySQL slow_query_log MySQL slow_query_log contains SQL statements that t

How to drop all tables in MySQL

Publish Date:2025/04/24 Views:196 Category:MySQL

This article demonstrates several ways for users to delete all tables in MySQL and lists sample scripts to implement this method. The reason you can't just drop all the tables in one line of code is that in a large and well-designed databas

Display tables and database structure in MySQL

Publish Date:2025/04/23 Views:97 Category:MySQL

Today, we will learn about queries in MySQL that can display the table and database structure. We will use the mysqldump utility, DESCRIBE the , SHOW TABLES and SHOW CREATE TABLE the statements. We are using MySQL version 8.0.28 while writi

Select first row from MySQL table

Publish Date:2025/04/23 Views:112 Category:MySQL

Today, we will explore three scenarios and their solutions where we want to select the first row from a MySQL table. In the first scenario, we will learn to get the first row from a MySQL table where there are multiple instances of a partic

Insert timestamp into MySQL table

Publish Date:2025/04/23 Views:78 Category:MySQL

Today, we will learn how to TIMESTAMP insert date and time into a type column of a MySQL table according to the table definition. Create a MySQL table First, we will create the tables that we will use in this tutorial. Sample code: CREATE T

The difference between two tables in MySQL

Publish Date:2025/04/23 Views:102 Category:MySQL

In this article, we will learn how to find the difference between two tables in MySQL. The difference between two tables in MySQL We often need to compare two tables to find records in one table that have no matching records in the other ta

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial