mysql - MySQL 中使用 WHERE EXISTS 进行删除查询

标签 mysql sql-delete

我正在使用 MySql 中的一个表执行“where contains”查询。它与 SELECT * 一起工作正常,但当我尝试执行 DELETE 而不是 SELECT * 时失败。

如何使用删除执行相同的查询?非常感谢!

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

最佳答案

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

关于mysql - MySQL 中使用 WHERE EXISTS 进行删除查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38754497/

相关文章:

mysql - 如何使用我的站点目录中的现有文件创建 WordPress 数据库文件?

mysql - 加快依赖于 BIT 列的 MySql DELETE

mysql - SQLSTATE[HY000] : General error with doctrine2 and no error

php - MySQL PDO - 设置默认获取模式?

mysql - 从文本中删除\n\r 的sql 查询是什么?

php - 从 MySQL 表中选择,按匹配的类别数排序

mysql - 收到错误 1451 (23000) : Cannot delete or update a parent row: a foreign key constraint fails

php - php 中的 DELETE sql 语句不起作用

php - 如果成员不再属于组,则从数据库中删除行

mysql - 如何防止gorm将自定义整数类型转换为字符串?