5

2 つのテーブルがあります (MySQL)

  1. data_details
  2. accounts_invoices

理想的には、すべての data_details に accounts_invoices ID が必要です。(data_details には、accounts_invoices の主キーを持つ外部キーがあります)

何らかの理由で、accounts_invoices テーブルに accounts_invoice_id が存在しない data_details レコードがあります。

そこで、data_details レコードを既知の accounts_invoice ID で更新しようとしました。これは私がしたことです

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)

誰かが私を助けてくれますか、事前に感謝します

乾杯

同時代

4

1 に答える 1

4

これは大げさな推測かもしれませんが、問題は、クエリを実行している同じテーブルを更新することだと思います。回避策は、次のような一時テーブルを使用することだと思います。

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
)
于 2011-06-01T08:13:48.690 に答える