缺少 table "t2" 的 FROM 子句条目

Missing FROM-clause entry for table "t2"

INSERT INTO patient_tbl(
patient_no, 
gender, 
nationality, 
dob, 
occupation) 
SELECT "t2".patient_no, 
t2.gender,
t2.nationality, 
t2.dob,
t2.occupation
FROM temp_patient_tbl t2 
ON CONFLICT (patient_no) DO UPDATE SET 
gender = t2.gender, 
nationality = t2.nationality, 
dob = t2.dob, 
occupation = t2.occupation;

我得到了这个错误:

ERROR: missing FROM-clause entry for table "t2" LINE 14: gender = t2.gender, ^

已经尝试和谷歌搜索了很长时间,但没有成功。

As documented in the manual 您需要使用关键字 excluded 来引用尝试插入的行值:

The SET and WHERE clauses in ON CONFLICT DO UPDATE have access [...] to rows proposed for insertion using the special excluded table.

INSERT INTO patient_tbl(patient_no, gender, nationality, dob, occupation) 
SELECT t2.patient_no, 
       t2.gender,
       t2.nationality, 
       t2.dob,
      t2.occupation
FROM temp_patient_tbl t2 
ON CONFLICT (patient_no) DO UPDATE SET 
  gender = EXCLUDED.gender, 
  nationality = EXCLUDED.nationality, 
  dob = EXCLUDED.dob, 
  occupation = EXCLUDED.occupation;