使用MySQL中的WHERE EXISTS删除查询

时间:2016-08-03 22:10:56

标签: mysql sql-delete

我正在使用&#34执行查询;其中存在"在MySql中使用一个表。它适用于SELECT *,但在我尝试执行DELETE而不是SELECT *时失败。

如何使用delete执行相同的查询?非常感谢提前!

select * from MyTable t1 where exists ( 
select * from MyTable t2 where t1.user_id = t2.user_id 
and t1.object_id <> t2.object_id and t2.role = "ADMIN")
and role = "ORG_MANAGER" 
and object_type = "type_b";

1 个答案:

答案 0 :(得分:1)

delete from MyTable t1 
where user_id in (
  select user_id 
  from MyTable t1 
  where exists ( 
    select * from MyTable t2 
    where t1.user_id = t2.user_id 
    and t1.object_id <> t2.object_id 
    and t2.role = "ADMIN")
  and role = "ORG_MANAGER" 
  and object_type = "type_b";
)