|
Custom Search
| |
|
How to do cross-table update in MySQL - Part 1 Cross table update in MySQL (also known as correlated update, or multiple table update) refers to the operation that updates records in one table based on whether they match or don't match records in another table. The matching uses the concept explained in the JOIN operation that we discussed in this section - Querying Multiple Tables. Please familiarize yourself with it if you haven't done so. MySQL can use either standard or non-standard (non ANSI standard) SQL syntax format to perform cross-table update. In this tutorial, we are going to look at four scenarios for MySQL cross table update. Suppose we have two tables Categories and Categories_Test. See screenshots below. The common column in the two tables is CategoryID. We are going to update column Description and Picture in table Categories_Test by using data in table Categories, based on data in the common column CategoryID.
Table: Categories
Table: Categories_Test Use the following script to create the categories_test table in MySQL Northwind database.
CREATE TABLE `categories_test` To repeatedly run the queries in this tutorial, use the following query to empty the data in the Description and Picture column after each update.
update categories_test
Please note that, unlike other DBMS such as SQL Server, Oracle, etc., when using UPDATE, MySQL will not update columns where the new value is the
same as the old value, and as such, you may see a message that indicates 0 records are updated even though there are records that match the UPDATE criteria.
1. Update data in a column in table A based on a common column in table B. Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page. 1.1 The update query below shows that the Picture column is updated by looking up the same ID value in CategoryID column in table Categories_Test and Categories. The join condition is specified in the WHERE clause. update categories_test as a, categories as b
In Categories_test table, picture column was updated for 8 records. 1.2 SQL below is equivalent to the one shown above. The difference is that it explicitly uses the keyword JOIN in the script. Also note that the join condition is specified in the INNER JOIN clause. update categories_test a 1.3 Here is a third way to do the same update. It uses the Oracle way of doing cross-table update. Note that the join condition is specified in the sub-query's WHERE clause. update Categories_Test a 2. Update data in two columns in table A based on a common column in table B. 2.1 When updating multiple columns, use comma to separate each column after the SET keyword. The join condition is specified in the WHERE clause. update categories_test as a, categories as b
In Categories_test table, both Description and Picture column were updated for 8 records. 2.2 Alternatively, the following SQL does the same thing but used the keyword JOIN explicitly and the join condition is specified in the INNER JOIN clause. update categories_test a 2.3 Here is a third way to do cross-table update for two columns. Note that the join condition is specified in the two sub-queries' WHERE clause. Performance wise it's not efficient but it works. update Categories_Test a Click here to view Part 2 of this tutorial. In Part 2, we'll introduce some complications into the query - adding conditional criteria in the WHERE clause as well as how to update multiple columns.
Happy Coding! Other tutorials in this category 1. Update Statement in MySQL |
Copyright © 2024 GeeksEngine.com. All Rights Reserved. This website is hosted by HostGator. No portion may be reproduced without my written permission. Software and hardware names mentioned on this site are registered trademarks of their respective companies. Should any right be infringed, it is totally unintentional. Drop me an email and I will promptly and gladly rectify it. |
Home | Feedback | Terms of Use | Privacy Policy |