加入删除

时间:2011-06-01 02:31:08

标签: mysql sql sql-delete

我正在使用mysql:

  • btableA有tableB_id列
  • tableB有some_interestring_column_on_TableB

我想要(下面的伪sql):

delete from tableA 
 where the associated row in tableB (via tableB_id) has  
     some_interestring_column_on_TableB = 'interestingValue'

请帮我把伪sql翻译成真正的sql。

3 个答案:

答案 0 :(得分:3)

试试这个:

 DELETE TableA
 WHERE tableB_id IN (
    SELECT id FROM TableB 
    WHERE  interestring_column='pizza');

答案 1 :(得分:2)

MySQL supports JOINs in the DELETE statement, as well as deleting from multiple tables in a single statement。以下内容仅从TABLEA中删除:

DELETE ta
  FROM TABLEA ta
  JOIN TABLEB tb ON b.id = a.tableb_id
                AND b.col = 'some value'

如果要从两个表中删除,请使用:

DELETE ta, tb
  FROM TABLEA ta
  JOIN TABLEB tb ON b.id = a.tableb_id
                AND b.col = 'some value'

也就是说,这种支持在其他数据库中非常罕见 - 在大多数情况下,您必须使用INEXISTS

答案 2 :(得分:0)

我不知道mysql语法,但我想的是(来自mssql):

delete from tableA 
where tableA.tableB_id in 
   (select tableB.id from tableB 
   where tableB.some_interesting_column_on_TableB = 'interestingValue')