在mysql中更新别名(Update with alias in mysql)

编程入门 行业动态 更新时间:2024-10-24 08:30:33
在mysql中更新别名(Update with alias in mysql)

我正在尝试更新记录使用别名,但它给了我错误。

SQL查询如下:

UPDATE file_associations as fa
SET fa.scheduled_charge_id = NULL 
WHERE fa.faid IN(
  SELECT faid
  FROM file_associations as fa
  JOIN maintenance_requests mr ON ( mr.cid = fa.cid
    AND mr.id = fa.maintenance_request_id ) 
  JOIN scheduled_charges sc ON ( sc.cid = fa.cid
    AND sc.scid = fa.scheduled_charge_id ) 
  WHERE fa.cid =235
    AND fa.scheduled_charge_id IS NOT NULL 
    AND fa.maintenance_request_id IS NOT NULL 
    AND fa.faid IN ( 1, 7 )
);
 

错误是:

#1093 - 您无法在FROM子句中为更新指定目标表'file_associations'

任何人都可以帮助我更新记录使用别名。

I am trying to update the record using alias but it gives me error.

The SQL query is the following:

UPDATE file_associations as fa
SET fa.scheduled_charge_id = NULL 
WHERE fa.faid IN(
  SELECT faid
  FROM file_associations as fa
  JOIN maintenance_requests mr ON ( mr.cid = fa.cid
    AND mr.id = fa.maintenance_request_id ) 
  JOIN scheduled_charges sc ON ( sc.cid = fa.cid
    AND sc.scid = fa.scheduled_charge_id ) 
  WHERE fa.cid =235
    AND fa.scheduled_charge_id IS NOT NULL 
    AND fa.maintenance_request_id IS NOT NULL 
    AND fa.faid IN ( 1, 7 )
);
 

Error is:

#1093 - You can't specify target table 'file_associations' for update in FROM clause

Can anybody please help me to update record using alias.

最满意答案

不要where in select中使用where in select而是使用join

update (select faid from file_associations as fa join maintenance_requests mr on ( mr.cid = fa.cid and mr.id = fa.maintenance_request_id ) join scheduled_charges sc on ( sc.cid = fa.cid and sc.scid = fa.scheduled_charge_id ) where fa.cid =235 and fa.scheduled_charge_id is not null and fa.maintenance_request_id is not null and fa.faid in ( 1, 7 ) ) s join file_associations as fa using (faid) set fa.scheduled_charge_id = null

don't use a where in select, use a join instead

update (select faid from file_associations as fa join maintenance_requests mr on ( mr.cid = fa.cid and mr.id = fa.maintenance_request_id ) join scheduled_charges sc on ( sc.cid = fa.cid and sc.scid = fa.scheduled_charge_id ) where fa.cid =235 and fa.scheduled_charge_id is not null and fa.maintenance_request_id is not null and fa.faid in ( 1, 7 ) ) s join file_associations as fa using (faid) set fa.scheduled_charge_id = null

更多推荐

本文发布于:2023-08-06 19:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1454965.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:别名   mysql   alias   Update

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!