简体   繁体   中英

Inner join and delete not working in mysql

I have two tables table1 and table2 . I want to delete from table1 based on a condition in table2 .

I have the following mysql query:

DELETE FROM table1 
INNER JOIN table2 ON table2.col1 = table1.col1
WHERE table2.col2 = '1'

This return a syntax error. Is there something wrong with the above syntax?

You need to specify the table you are deleting from:

DELETE table1
    FROM table1 INNER JOIN
         table2
         USING (col1) 
    WHERE table2.col2 = '1';

Try this:

DELETE FROM table1
WHERE EXISTS(
    SELECT 'C'
    FROM table2
    WHERE table2.col1 = table1.col1
    AND table2.col2 = '1'
)

您可以执行以下操作:

DELETE FROM table1 WHERE col1 IN (select col1 from table2 WHERE table2.col2 = '1');

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