【问题标题】:Update with not in in clause in mysql在mysql中使用not in子句更新
【发布时间】:2016-10-02 06:56:20
【问题描述】:

从我的角度来看,这个查询应该可以工作

UPDATE tab_antiguedad set rol_id = 4 
where ant_id = 29 
      and rol_id not in (
                         select rol_id 
                         from tab_antiguedad 
                         where usu_id = 55
                        )

Where (select rol_id from tab_antiguedad where usu_id = 55) 这里rol_id 有 4 和 3。

但我犯的错误是:

您不能在 FROM 中指定目标表 'tab_antiguedad' 进行更新 子句

请帮忙。

【问题讨论】:

    标签: mysql sql-update where-clause


    【解决方案1】:

    尝试使用别名

    UPDATE tab_antiguedad as t1, (select rol_id 
            from tab_antiguedad   where usu_id = 55) as t2
    set t1.rol_id = 4 
    where t1.ant_id = 29 
    and t1.rol_id != t2.rol_id 
    

    【讨论】:

    • 给出以下错误:您不能在 FROM 子句中指定目标表 't1' 进行更新
    • 感谢 ScaisEdge,但奇怪的是,如果 t1.rol_id 为 3 或 4,“不在”应该不允许更新,通过允许更新?
    • 好的,我需要你在“不在”中比较所有 rol_id 如果存在不应该升级任何东西。
    • 可能不存在......我已经更新了答案..否则用正确、清晰的样本更新你的问题..
    • 问题是 t2.rol_id 应该显示“4 和 3”时才显示“4”
    【解决方案2】:

    问题是您无法更新您在选择部分中使用的同一个表。

    更新tab_antiguedad

    设置 rol_id = 4

    其中 ant_id = 29 和

    rol_id 不在(select rol_id from tab_antiguedad where usu_id = 55)

    【讨论】:

    • 那么解决办法是什么?
    • 我对子查询不太了解,因为我主要使用简单的 SQL 命令,但我发现了一个类似的问题,我认为这可以帮助你。只需点击THIS 即可打开它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-12
    • 2019-08-10
    • 1970-01-01
    相关资源
    最近更新 更多