mysql - 查询中有什么问题

标签 mysql

我正在尝试触发以下查询...

有什么问题吗?

delete from user_role 
WHERE user_id in (
    select u.user_id from user u, user_role ur 
    where u.USER_ID=ur.USER_ID and ur.ROLE_ID=4 and u.USER_ID not in (
        select user_id from referrers));

最佳答案

如手册部分所述DELETE Syntax :

Currently, you cannot delete from a table and select from the same table in a subquery.

您可以改为使用 DELETEjoin 的多表形式把你的 table 放在一起:

DELETE user_role
FROM   user_role
  INNER JOIN user_id   USING (USER_ID)
  LEFT  JOIN referrers USING (USER_ID)
WHERE user_role.ROLE_ID = 4 AND referrers.USER_ID IS NULL

关于mysql - 查询中有什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11339368/

相关文章:

Mysql - 左连接所有表

mysql - Sphinx 为 mysql 主键自增 id 创建属性

Mysql插入查询失败

mysql - 对多个计数字段求和

php - bool 值在mysql中另存为tinyint(1)

c# - 在 docker 机器上调试时 MySql 连接被拒绝

Mysql按RAND排序,但必须包含少量数据

mysql - 部分恢复

mysql - 从相关的两个表中查找重复项

mysql - 将 INNER JOIN 和 LEFT JOIN 与日期选择结合起来