• A2-02-28.DML-MySQL UPDATE JOIN


    转载自:http://www.mysqltutorial.org/mysql-update-join/

    MySQL UPDATE JOIN

     

    Summary: in this tutorial, you will learn how to use the MySQL UPDATE JOIN statement to perform the cross-table update. We will show you step by step how to use INNER JOIN  clause and LEFT JOIN  clause with the UPDATE statement.

    MySQL UPDATE JOIN syntax

    We often use join clauses to query rows in a table that have (in the case of INNER JOIN) or may not have (in the case of LEFT JOIN) corresponding rows in another table. In MySQL, we can use the JOIN clauses in the UPDATE statement to perform the cross-table update.

    The syntax of the MySQL UPDATE JOIN  is as follows:

    Let’s examine the MySQL UPDATE JOIN  syntax in greater detail:

    • First, you specify the main table ( T1 ) and the table that you want the main table to join to ( T2 ) after the UPDATE clause. Notice that you must specify at least one table after the UPDATE  clause. The data in the table that is not specified after the UPDATE  clause is not updated.
    • Second, you specify a kind of join you want to use i.e., either INNER JOIN  or LEFT JOIN  and a join condition. The JOIN clause must appear right after the UPDATE clause.
    • Third, you assign new values to the columns in T1 and/or T2 tables that you want to update.
    • Fourth, the condition in the WHERE clause allows you to specify which rows to update.

    If you follow the UPDATE statement tutorial, you notice that there is another way to update data cross-table using the following syntax:

    This UPDATE  statement works the same as UPDATE JOIN  with implicit INNER JOIN  clause. It means you can rewrite the above statement as follows:

    Let’s take a look at some examples of using the UPDATE JOIN  statement to having a better understanding.

    MySQL UPDATE JOIN examples

    We are going to use a new sample database in these examples. The sample database contains 2 tables:

    • The  employees table stores employee data with employee id, name, performance, and salary.
    • The merits table stores employee performance and merit’s percentage.

    The following statements create and load data in the empdb sample database:

    MySQL UPDATE JOIN example with INNER JOIN clause

    Suppose you want to adjust the salary of employees based on their performance.

    The merit’s percentages are stored in the merits table therefore, you have to use the UPDATE INNER JOINstatement to adjust the salary of employees in the employees  table based on the percentage stored in the merits table.

    The link between the employees  and merit tables is the performance  field. See the following query:

    MySQL Update Join Example

    How the query works.

    We specify only the employees table after UPDATE clause because we want to update data in the employees table only.

    For each row in the employees table, the query checks the value in the performance column against the value in the performance column in the merits table. If it finds a match, it gets the percentage in the merits  table and updates the salary column in the employees  table.

    Because we omit the WHERE clause in the UPDATE  statement, all the records in the employees  table get updated.

    MySQL UPDATE JOIN example with LEFT JOIN

    Suppose the company hires two more employees:

    Because these employees are new hires so their performance data is not available or NULL .

    MySQL Update Left Join Example

    To increase the salary for new hires, you cannot use the UPDATE INNER JOIN  statement because their performance data is not available in the merit  table. This is why the UPDATE LEFT JOIN  comes to the rescue.

    The UPDATE LEFT JOIN  statement basically updates a row in a table when it does not have a corresponding row in another table.

    For example, you can increase the salary for a new hire by 1.5%  using the following statement:

    MySQL Update Left Join with Example

    In this tutorial, we have shown you how to use the MySQL UPDATE JOIN  with the INNER JOIN  and LEFT JOIN  clauses to perform the cross-table update.

  • 相关阅读:
    MySQL高级查询总结
    MySQL数据库作业
    MySQLdump备份还原命令
    MySQL之Join
    MySQL课堂作业(一)
    Mysql数据库
    Js实例之简易计算器
    JS系统函数
    js课堂作业之转换月份
    C++ Name Mangling 为什么不编码返回值参数
  • 原文地址:https://www.cnblogs.com/zhuntidaoren/p/9519468.html
Copyright © 2020-2023  润新知