简体   繁体   中英

Whats wrong with MySQL query?

I am trying to delete rows from a table that spit out from a join:

  DELETE FROM t1 WHERE company_name IN 
    (SELECT company_name FROM t1
        LEFT OUTER JOIN t2
        ON t2.company_name = t1.company_name
        WHERE t2.name IS null)

Column 'company_name' in field list is ambiguous

Getting this ambiguous error while trying to make this query? Any suggestions?

MySQL doesn't like it when you try to UPDATE / DELETE a table and SELECT from the same table in the same query.

You can solve this with multi-table DELETE syntax:

DELETE t1 FROM t1 LEFT OUTER JOIN t2 USING (company_name)
WHERE t2.name IS NULL;

试试看

   DELETE FROM t1 WHERE company_name IN ( select * from (SELECT t1.company_name FROM t1 LEFT OUTER JOIN t2 ON t2.company_name = t1.company_name WHERE t2.name IS null) t )

As the error message tells the column name company_name is not unique. Depending on your needs I believe this may solve your problem assuming you're trying to delete entries in t1 which doesn't have a corresponding row in t2 or have one but with name is null:

DELETE FROM
    t1
WHERE
    company_name NOT IN (
        SELECT
            t2.company_name
        FROM
            t2
        WHERE
            t2.name IS NOT NULL
    )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM