Remove duplicate data from mysql

Source: Internet
Author: User
Tags mysql delete

Select can take alias, delete cannot.

1. When you use mysql to perform the delete from operation, if the FROM Statement of the subquery and the update/delete object use the same table, an error will occur.

Mysql> delete from tab1 WHERE col1 = (select max (col1) FROM tab1 );
ERROR 1093 (HY000): You can't specify target table 'tab1' for update in FROM clause

In most cases, you can add a select alias table to solve the "same table" restriction.

Delete from tab1
WHERE col1 = (
Select max (col1)
FROM (
SELECT * FROM tab1
) AS t
);

 

------------------------------------------------------------------------

2. When mysql delete from where in is followed by a query statement, the where condition cannot be added.

 

SQL code
Delete from 't_ goods 'where fi_id in (select * from (select fi_id from 't_ goods' where fs_num is null and fs_name is null and fs_type is null andfs_using is null and fs_lifetime is null) b)

 

SQL code
Delete from 't_ goods 'where fi_id in (select fi_id from 't_ goods' where fs_num is null and fs_name is null and fs_type is null andfs_using is null and fs_lifetime is null)

 

SQL code
Delete from 't_ goods 'where fi_id in (select fi_id from 't_ goods ')

In the preceding three cases, only the intermediate one cannot be executed.

In general, the where condition cannot be added to the query statement after mysql delete from where in.

---------------------------------------------------------------------------

 

3. delete from table... the table cannot use an alias.

 SQL code

Delete from student a where a. id in (1, 2); (execution failed)

Select a. * from student a where a. id in (1, 2); (executed successfully)


Delete from v9_news WHERE title IN (SELECT title FROM v9_news as newsb WHERE FROM_UNIXTIME (newsb. inputtime, '% Y-% m-% D') = '2017-06-27' group by newsb. title having count (*)> 1) B)
AND id not in (SELECT id FROM (select min (id) AS id FROM v9_news WHERE FROM_UNIXTIME (inputtime, '% Y-% m-% D ') = '1970-06-27 'group by title having count (*)> 1) c); TRUE !!
Delete from v9_news AS news
WHERE news. title IN (SELECT title FROM v9_news as newsb WHERE FROM_UNIXTIME (newsb. inputtime, '% Y-% m-% D') = '2017-06-27' group by newsb. title having count (*)> 1)
AND id not in (select min (id) FROM v9_news WHERE FROM_UNIXTIME (inputtime, '% Y-% m-% D ') = '1970-06-27 'group by title having count (*)> 1); FALSE !!

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.