2017-08-30 53 views
0

請幫幫忙,PostgreSQL的:從選擇更新總是失敗

我會在account_move_line(列tax_number)與價值account_invoice_line(列no_pajak)更新的價值。 但它總是返回

這是我的代碼:

update account_move_line aml set tax_number = (
    select no_pajak as tax_number from account_invoice_line 
    where invoice_id = aml.invoice_id and no_pajak is not NULL 
) 
where tax_number is NULL and date > '2017-08-01' 

非常感謝您

+0

代碼應該工作。請提供表格中的樣本數據和預期結果。 –

回答

0

我不知道是否可以使用橫向加入這種方式,因此與CTE重寫了它:

with u as (
    select no_pajak, invoice_id id 
    from account_invoice_line 
    join account_move_line aml on invoice_id = aml.invoice_id and no_pajak is not NULL 
) 
update account_move_line aml set tax_number = no_pajak 
from u 
where invoice_id = id 
and tax_number is NULL and date > '2017-08-01' 

應該工作,雖然看起來過於複雜