DELETE query with WHERE EXISTS in MySQL [duplicate]

狂风中的少年 提交于 2019-12-11 05:50:11

问题


I am executing the query with "where exists" using one table in MySql. It works fine with SELECT *, but fails when I try to do DELETE instead of SELECT *.

How to perform the same query with delete? Many thanks in advance!

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:


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";
)


来源:https://stackoverflow.com/questions/38754497/delete-query-with-where-exists-in-mysql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!