加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

MySQL批量更新

发布时间:2020-12-11 23:48:36 所属栏目:MySql教程 来源:网络整理
导读:我有2个表(MySQL) data_details accounts_invoices 理想情况下,每个data_details都应具有accounts_invoices id. (data_details有一个带有accounts_invoices主键的外键) 由于某种原因,有data_details记录,其中accounts_invooice表中不存在accounts_invoice_id

我有2个表(MySQL)

> data_details
> accounts_invoices

理想情况下,每个data_details都应具有accounts_invoices id. (data_details有一个带有accounts_invoices主键的外键)

由于某种原因,有data_details记录,其中accounts_invooice表中不存在accounts_invoice_id

所以我尝试用已知的accounts_invoice id更新那些data_details记录.这就是我做的

update data_details 
set account_invoice_id = 1
where account_invoice_id in (
  select d.id
  from data_details d left join accounts_invoices a
  on d.account_invoice_id = a.id
  where a.id is null    
)

但是说出错了

您可以在FROM子句中指定目标表’data_details’以进行更新(错误1093)

有人可以帮助我,提前谢谢

干杯

sameera

最佳答案 现在这可能是一个疯狂的猜测,但我认为问题是你更新了你正在查询的同一个表.我认为解决方法是使用临时表,如下所示:

update data_details 
set account_invoice_id = 1
where account_invoice_id in (
select * from (
  select d.id
  from data_details d left join accounts_invoices a
  on d.account_invoice_id = a.id
  where a.id is null    
) as t
)

虽然没试过,所以可能都错了.

更新了SQL以修复我在注释中发现的错误.

update data_details 
set account_invoice_id = 1
where id in (
select * from (
  select d.id
  from data_details d left join accounts_invoices a
  on d.account_invoice_id = a.id
  where a.id is null    
) as t
)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读