MySQL selects and updates the same table

Source: Internet
Author: User

MySQL does not allow you to run select while updating a table

The work und is to generate a temporary table from a derived table. In this way, both update and select

Update tb_trade_1 inner join (select tid, count (*) as CNT from tb_order_1 group by TID) as der using (TID) set commission_limit = der. CNT;

Remove duplicate data in MySQL

In general, the SQL statement should be:
Delete tablename where id not in (select Min (ID) from tablename group by name, kecheng, fenshu );
This method is supported in sqlserver or Oracle, but MySQL does not currently support it. An error similar to the following is reported: You can't specify target table 'tablename' for update, this is because MySQL cannot query the data of a table at the same time and then delete it at the same time.

Delete A. * From tb_trade_0 as A, (select tid, count (*), min (ID) as ID from tb_trade_0
Group by TID having count (*)> 1 order by gmt_create DESC) as B
Where a. tid = B. TID and A. ID> B. ID;

Delete. * From 'sites _ Cates 'as a left join 'sites' as B on 'A '. 'site _ id' = 'B '. 'sid 'where' B '. 'name' is null is not simple. The difference between MySQL and Oracle is that MySQL cannot use the table in the clause, but oracle can, this is one of the differences between MySQL and Oracle SQL.

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.