【问题标题】:i am having a problem with not in function in mysql, here is the query我在mysql中没有功能时遇到问题,这是查询
【发布时间】:2022-01-29 06:02:12
【问题描述】:
delete from emploee_id where id not in(select max(id) from emploee_id group by name,dept,salary,experience);

在运行此查询时,错误显示如下

“错误代码:1093。您不能为目标表'emploee_id'指定 在 FROM 子句中更新"

请有人帮帮我

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    您可以改为对临时表执行 JOIN

    delete e 
    from emploee_id e
    left join
    (
      select select max(id) as id
      from emploee_id 
      group by name, dept, salary, experience
    ) tmp on tmp.id = e.id
    where tmp.id is null
    

    【讨论】:

      【解决方案2】:

      您不能在子查询中使用同一个表。尝试使用 SET @variable_name = select max(id) from emploee_id group by name,dept,salary,experience作为单独的查询,并在变量中设置其值。 然后在 delete query delete from emploee_id where id=@variable_name

      中使用该值

      【讨论】:

      • 子查询不一定只返回一行,因此在大多数情况下将其分配给标量变量是行不通的。
      【解决方案3】:
      "delete from emploee_id where id not in(select max(id) from emploee_id group by name,dept,salary,experience); "
      

      实际上没有目标,该 select 语句中应该有一个 where 子句 例子

      "delete from emploee_id where id not in(select max(id) from emploee_id **where** group by name,dept,salary,experience); "
      

      我真的认为您再次查看 sql 选择的代码并确定您要查询的内容

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-11
        • 1970-01-01
        • 2018-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多