---
url: /dev/f64t95az/index.md
description: MySQL 同表查询更新示例，使用临时表解决同表数据查询后更新的问题，避免 You can't specify target table 错误。
---
这里的内容会被作为摘要

```sql
CREATE
TEMPORARY TABLE temp_loan_amount_table AS
SELECT fca.uuid, (fca.amount - fca.interestAmount) AS newLoanAmount
FROM Asset fca
WHERE fca.stateEnum = 'PASS_PAY'
  AND (fca.)
  AND (fca.loanAmount IS NULL OR fca.loanAmount = 0);
UPDATE CoreAsset fca JOIN temp_loan_amount_table tt
ON fca.uuid = tt.uuid
    SET fca.loanAmount = tt.newLoanAmount
WHERE fca.stateEnum = 'PASS_PAY'
  AND (fca.loanAmount is null
   OR fca.loanAmount = 0);

DROP
TEMPORARY TABLE temp_loan_amount_table;
```
