Search code examples
mysqlsqlsql-delete

SQL Delete with Subquery


I'm trying to delete from a table with the results of a subquery. The results return a unique tuple, and currently I end up deleting more than just the results returned because i'm only checking col1 results.

DELETE FROM Table1 exTable
WHERE exTable.col1 = ... AND exTable.col2 = ...
(SELECT col1, col2
FROM ...)

Solution

  • Use a join to match more than 1 column.

    DELETE t1
    FROM Table1 t1
    inner join 
    (
       select col1, col2 
       from other_table 
       where ...
    ) t2 on  t2.col1 = t1.col1 
         and t2.col2 = t1.col2