删除加入同一个表

时间:2009-01-09 09:47:41

标签: mysql

是否可以执行连接同一个表的删除查询语句, 我尝试了各种连接(内部,左侧),但没有运气mysql返回错误

我需要的例子:

DELETE `a` FROM `t1` AS `a`
INNER JOIN `t1` AS `b` USING `some_field_b`
WHERE 
    `a`.`some_field_a` = 'value_x' AND 
    `b`.`some_field_a` = 'value_y'

1 个答案:

答案 0 :(得分:3)

虽然manual似乎暗示INNER JOIN语法应该在DELETE中工作,但我知道这个带有join子句的替代方法移动到where条件会起作用....

DELETE  a.* FROM t1 AS a, t1 as b 
WHERE 
    a.some_field_b=b.some_field_b AND
    a.some_field_a = value_x AND 
    b.some_field_a = value_y

编辑:我刚试过这个,对我有用:

DELETE a FROM t1 AS a 
INNER JOIN t1 as b USING(some_field_b) 
WHERE 
    a.some_field_a = value_x AND 
    b.some_field_a = value_y