How to use join for delete clause in mysql?

Member

by aubrey , in category: MySQL , a year ago

How to use join for delete clause in mysql?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by mallory_cormier , 10 months 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.

Related Threads:

How to use like in where clause in Codeigniter?
How to use with clause with union all in oracle sql?
How to use mysql instead of mariadb in symfony 5?
How to use class in like clause in teradata?
How to join 4 tables with mysql?
How to add where condition in left join in mysql?