About the update set from where

Source: Internet
Author: User

About Update Set fromwhere

Here's an example:

Two Tables A, B, to make the Memo field value in B equal to the name value of the corresponding ID in table a   
  table A:id,name   
        & nbsp   1       Wang   ,
            2       Li  & nbsp
            3       Zhang   
  Table b:id,clientname   &NBSP
            1         
            2   
            3    (MS SQL Server) statement: update    b   set    ClientName    =   a.name    from    a,b   where   a.id   =   b.id  

(ORALCE) Statement:update b set (ClientName) = (SELECT name from a WHERE b.id = a.id)

(Mysql) Statement:: UPDATE A, B SET A1 = B1, A2 = B2, A3 = B3 WHERE a.id = b.id

Update Set from statement format

When both where and set need to correlate a table for querying, the entire update executes, requiring two scans of the associated table, which is obviously less efficient.
For this scenario, the workaround for Sybase and SQL Server is to use the UPDATE... SET... from ... WHERE ... Syntax, which is actually getting updated data from the source table.

In SQL, table joins (left joins, right joins, Inner joins, and so on) are often used in SELECT statements, in the case of SQL syntax, which can also be used for update and DELETE statements, using J in these statements Oin also often get a multiplier effect.

Update T_orderform SET t_orderform.sellerid =b.l_tuserid
from T_orderform A left JOIN t_productinfo B on B.l_id=a.productid

Used to synchronize data from two tables!

Syntax supported by both ORALCE and DB2:

UPDATE A SET (A1, A2, A3) = (SELECT B1, B2, B3 from B WHERE a.id = b.id)

MS SQL Server does not support such a syntax, the corresponding wording is:
UPDATE A SET A1 = B1, A2 = B2, A3 = B3 from a left JOIN B on a.id = b.ID


Personal feeling MS SQL Server's Update syntax is more powerful. MS SQL Server notation:
UPDATE A SET A1 = B1, A2 = B2, A3 = B3 from A, B WHERE a.id = b.id

The syntax for Oracle and DB2 is more cumbersome, as follows:

UPDATEA SET(A1, A2, A3) = (SELECT B1, B2, B3 fromB WHERE a.id = b.id)
WHERE ID in (SELECT b.id fromB WHERE a.id = b.id)

The syntax for MySQL is:

UPDATE A, B SET A1 = B1, A2 = B2, A3 = B3 WHERE a.id = b.id

About the update set from where

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.