列名在 UPSERT 操作中不明确
Column name is ambiguous on UPSERT action
我正在尝试 运行 postgres 数据库上的 UPSERT
查询字符串 -
insert into business_data_test
(select * from business_data_tmp)
on conflict(phone_number)
do update set
average_expense=(average_expense*expense_count + excluded.average_expense*excluded.expense_count)/(expense_count + excluded.expense_count), expense_count=(expense_count + excluded.expense_count);
如果存在数据冲突,我基本上会尝试更新列 average_expense
,但我认为查询有问题,因为我 运行 遇到以下错误 -
ERROR: column reference "average_expense" is ambiguous
LINE 1: ...lict(phone_number) do update set average_expense=(average_ex...
^
SQL state: 42702
Character: 123
我认为我们必须在某处添加一些 table 名称别名,但我不确定如何解决这个问题。
您的查询已“排除”。但没有提及“排除”是什么?
我的建议是,使用单个列值而不是冗长的计算逻辑来进行更新。确保你让它工作然后添加到该更新逻辑....
这样更容易调试。
您需要完全限定对旧值(即“未排除的”值)的引用。如果您为目标使用别名,这会更容易一些 table
insert into business_data_test as tst
select *
from business_data_tmp
on conflict(phone_number)
do update
set average_expense = (tst.average_expense * tst.expense_count + excluded.average_expense * excluded.expense_count)/(tst.expense_count + excluded.expense_count),
expense_count = tst.expense_count + excluded.expense_count;
我正在尝试 运行 postgres 数据库上的 UPSERT
查询字符串 -
insert into business_data_test
(select * from business_data_tmp)
on conflict(phone_number)
do update set
average_expense=(average_expense*expense_count + excluded.average_expense*excluded.expense_count)/(expense_count + excluded.expense_count), expense_count=(expense_count + excluded.expense_count);
如果存在数据冲突,我基本上会尝试更新列 average_expense
,但我认为查询有问题,因为我 运行 遇到以下错误 -
ERROR: column reference "average_expense" is ambiguous
LINE 1: ...lict(phone_number) do update set average_expense=(average_ex...
^
SQL state: 42702
Character: 123
我认为我们必须在某处添加一些 table 名称别名,但我不确定如何解决这个问题。
您的查询已“排除”。但没有提及“排除”是什么?
我的建议是,使用单个列值而不是冗长的计算逻辑来进行更新。确保你让它工作然后添加到该更新逻辑....
这样更容易调试。
您需要完全限定对旧值(即“未排除的”值)的引用。如果您为目标使用别名,这会更容易一些 table
insert into business_data_test as tst
select *
from business_data_tmp
on conflict(phone_number)
do update
set average_expense = (tst.average_expense * tst.expense_count + excluded.average_expense * excluded.expense_count)/(tst.expense_count + excluded.expense_count),
expense_count = tst.expense_count + excluded.expense_count;