Use Inner join in SQL update Delete

Source: Internet
Author: User
Update

The update XXX set XXX Where statement is well known. Only then can we find that the update and delete statements support the inner join update method. This is very useful in Table Association for update and delete operations.

Column:

Update Tb_user
Set Pass = ''
From Tb_user USR
Inner Join Tb_address ADDR On USR. naddressfk = ADDR. naddressid
Where USR. ID = 123

 The update format is

Update T1 set t1.name = 'liu'From T1Inner join T2 on t1.id = t2.tid


MySQL and access are written as follows:

Update Mem_world As Mw1 Inner Join Mem_world As Mw2
On Mw1.parentid = Mw2.wid
Set Mw1. Level = Mw2. Level
Where Mw2.baseid = 107
And Mw2.parentid = 0
And Mw2.size > 1 ;

 

On is the filtering condition for table connections.
That is to say, after a table is connected, something similar to a temporary view will be generated.
Where filters data from this temporary view.
Therefore, you must first find out which of your so-called two conditions belong.

Delete

The delete statement is similar

Delete from T1From T1Inner join T2 on t1.id = t2.tid

Note:Blue.

MySQL:

Delete Mwb From Mem_world_building As Mwb Inner Join Mem_world As MW
On Mwb. wid = MW. wid
Where MW. Type Between 11 And 15
And Baseid = 107
And MW. parentid <> 0
And MW. Size > 1 ;

 

The following is Oracle: Delete Table1 Where Exists ( Select 1 From Table 2 Where And Table1.khid = Table2.khid And Fwdwid = 8 );

 

Delete Table1 Where Khid Exists ( Select Khid From Table 2 Where Fwdwid = 8 )

Original article: http://nodonkey.iteye.com/blog/456403

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.