這是可能的Postgres裏也the main difference is
Note that the target table must not appear in the from_list
, unless you intend a self-join (in which case it must appear with an alias in the from_list
).
您的查詢轉換:
UPDATE "Before" "T1"
SET "T1"."Amount" = ("T1"."Amount" - "T2"."Disc") + (("DT1"."N_Amount")/2)
FROM "Before" "T2"
LEFT JOIN "DiscTable" "DT1" ON "T1"."PurchID" = "DT1"."Purch_ID"
WHERE "T1"."ID" = "T2"."ID"
但爲什麼在這裏使用自加入? (如果"ID"
是主鍵),我想你可以achive你的目標可以簡單地使用:
UPDATE "Before" "T1"
SET "T1"."Amount" = ("T1"."Amount" - "T1"."Disc") + (("DT1"."N_Amount")/2)
FROM "DiscTable" "DT1"
WHERE "T1"."PurchID" = "DT1"."Purch_ID"
編輯:about quoting:
Quoting an identifier also makes it case-sensitive, whereas unquoted names are always folded to lower case. For example, the identifiers FOO, foo, and "foo" are considered the same by PostgreSQL, but "Foo" and "FOO" are different from these three and each other. (The folding of unquoted names to lower case in PostgreSQL is incompatible with the SQL standard, which says that unquoted names should be folded to upper case. Thus, foo should be equivalent to "FOO" not "foo" according to the standard. If you want to write portable applications you are advised to always quote a particular name or never quote it.)
但它顯示錯誤:無效引用FROM子句表「t1」的條目 – user1690835
如果您在表格/列名稱中混合使用大小寫字母,請使用無處不在或無處引用。如果您不引用postgresql標識符,它將轉換爲小型大寫字母以實現iso sql不區分大小寫。 http://www.postgresql.org/docs/9.3/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS – pozs