How to use join for delete clause in mysql?

Member

by aubrey , in category: MySQL , 5 months ago

How to use join for delete clause in mysql?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by mallory_cormier , a month ago

@aubrey 

You cannot use the JOIN clause directly in a DELETE statement in MySQL. However, you can achieve the same result by using a subquery in the WHERE clause. Here is an example:

1
2
3
4
5
6
7
DELETE FROM table1
WHERE id IN (
    SELECT id
    FROM table1
    JOIN table2 ON table1.id = table2.id
    WHERE condition
);


In this example, replace table1, table2, id and condition with your actual table names, join condition and filter criteria. This will delete rows from table1 based on the join condition with table2.


Make sure to test your DELETE statement with a SELECT query first to ensure that you are deleting the correct rows.