In mysql, You can't specify target table for update in FROM clause. specifyclause

Source: Internet
Author: User

In mysql, You can't specify target table for update in FROM clause. specifyclause

In mysql, You can't specify target table for update in FROM clause errors mean that some values in the same table cannot be selected first, update the table (in the same statement ). For example, the following SQL statement:
Copy codeThe Code is as follows:
Delete from tbl where id in
(
Select max (id) from tbl a where EXISTS
(
Select 1 from tbl B where a. tac = B. tac group by tac HAVING count (1)> 1
)
Group by tac
)

Rewrite it to the following:

Copy codeThe Code is as follows:
Delete from tbl where id in
(
Select a. id from
(
Select max (id) id from tbl a where EXISTS
(
Select 1 from tbl B where a. tac = B. tac group by tac HAVING count (1)> 1
)
Group by tac
)
)

That is to say, the result of the select statement is then passed through the select statement in the middle table, thus avoiding errors. Note that this problem only occurs in mysql, and does not occur in mssql and oracle.

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.