zoukankan      html  css  js  c++  java
  • SQL Server UPDATE JOIN

    Summary: in this tutorial, you will learn how to use the SQL Server UPDATE JOIN statement to perform a cross-table update.

    SQL Server UPDATE JOIN syntax

    To query data from related tables, you often use the join clauses, either inner join or left join. In SQL Server, you can use these join clauses in the UPDATE statement to perform a cross-table update.

    The following illustrates the syntax of the UPDATE JOIN clause:

    In this syntax:

    • First, specify the name of the table (t1) that you want to update in the UPDATE clause.
    • Next, specify the new value for each column of the updated table.
    • Then, again specify the table from which you want to update in the FROM clause.
    • After that, use either INNER JOIN or LEFT JOIN to join to another table (t2) using a join predicate specified after the ON keyword.
    • Finally, add an optional WHERE clause to specify rows to be updated.

    SQL Server UPDATE JOIN examples

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

    Setting up sample tables

    First, create a new table named sales.targets to store the sales targets:

    If sales staffs achieved the target 1, they will get the ratio of 0.2 or 20% sales commission and so on.

    Second, create another table named sales.commissions to store the sales commissions:

    The sales.commissions table stores sales staff identification, target_idbase_amount, and commission. This table links to the sales.targets table via the target_id column.

    Our goal is to calculate the commissions of all sales staffs based on their sales targets.

    A) SQL Server UPDATE INNER JOIN example

    The following statement uses the UPDATE INNER JOIN to calculate the sales commission for all sales staffs:

    Here is the output:

    If you query the sales.commissions table again, you will see that the values in the commission column are updated:

    SQL Server UPDATE JOIN - INNER JOIN example

    B) SQL Server UPDATE LEFT JOIN example

    Suppose we have two more new sales staffs that have just joined and they don’t have any target yet:

    We assume that the commission for the new sales staffs is 0.1 or 10%, we can update the commission of all sales staffs using the UPDATE LEFT JOIN as follows:

    In this example, we used COALESCE() to return 0.1 if the percentage is NULL.

    Note that if you use the UPDATE INNER JOIN clause, just the five rows of the table whose targets are not NULL will be updated.

    Let’s examine the data in the sales.commissions table:

    The result set is as follows:

    SQL Server UPDATE JOIN - LEFT JOIN example

    In this tutorial, you have learned how to use the SQL Server UPDATE JOIN statement to perform a cross-table update.

  • 相关阅读:
    poj 1984 Navigation Nightmare(带权并查集+小小的技巧)
    zoj 3261 Connections in Galaxy War(并查集逆向加边)
    poj 1733 Parity game(带权并查集)
    poj 1456 Supermarket(贪心+优先队列)
    hdu 3038 How Many Answers Are Wrong(并查集的思想利用)
    poj 1182 食物链(种类并查集 ‘初心者’)
    hdu 1182 A Bug's Life(简单种类并查集)
    hdu 4725 The Shortest Path in Nya Graph(建图+优先队列dijstra)
    CodeForces 779D. String Game(二分答案)
    poj 3169 Layout(差分约束+spfa)
  • 原文地址:https://www.cnblogs.com/oxsir/p/12071459.html
Copyright © 2011-2022 走看看