MySQL database update sub-query, mysql database update

Source: Internet
Author: User

MySQL database update sub-query, mysql database update

For example:

UPDATE test. tb_vobile
Set a. name = '20140901'
WHERE
A. id = (select max (id) id from test. tb_vobile)

Error:

[SQL] UPDATE test. tb_vobile
Set a. name = '20140901'
WHERE
A. id = (select max (id) id from test. tb_vobile)


[Err] 1093-You can't specify target table 'A' for update in FROM clause


You can:

UPDATE test. tb_vobile
Join
(Select max (id) id from test. tb_vobile) B
On a. id = B. id
Set a. name = '20140901 ';
Or

UPDATE test. tb_vobile a, (select max (id) id from test. tb_vobile) B
Set a. name = '20140901'
WHERE
A. id = B. id;

Note:

1. During update, the updated table cannot be used in set and where for subqueries;
2. During update, you can update multiple tables (not SQL Server );
For example, update ta a, tb B set a. Bid = B. id, B. Aid = a. id;
3. Any query can be performed after update, which is equivalent to from;

Related Article

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.