Postgres Update mit einem Inner Join über 2 Tabellen?
Ich habe 3 Tabellen in meiner lokalen Postgres-Datenbank:
[myschema].[animals]
--------------------
animal_id
animal_attrib_type_id (foreign key to [myschema].[animal_attrib_types])
animal_attrib_value_id (foreign key to [myschema].[animal_attrib_values])
[myschema].[animal_attrib_types]
--------------------------------
animal_attrib_type_id
animal_attrib_type_name
[myschema].[animal_attrib_values]
--------------------------------
animal_attrib_value_id
animal_attrib_value_name
Zur Laufzeit werde ich das kennenanimal_id
. Ich muss SQL ausführen, um das zu aktualisierenanimal_attribute_value_name
mit diesem Artikel verbunden, so etwas wie:
UPDATE
animal_attribute_values aav
SET
aav.animal_attribute_value_name = 'Some new value'
WHERE
# Somehow join from the provided animal_id???
Möglicherweise muss ich eine Art verschachteltes tunSELECT
oderINNER JOIN
in derWHERE
Klausel, aber nicht sicher, wie das geht. Danke im Voraus!
Bearbeiten:
Sagen wir, ich habe eineanimal
Mit folgenden Werten aufnehmen:
[myschema].[animals]
--------------------
animal_id = 458
animal_attrib_type_id = 38
animal_attrib_value_id = 23
Und das entsprechendeanimal_attrib_value
(mit id = 23) hat die folgenden Werte:
[myschema].[animal_attrib_values]
--------------------------------
animal_attrib_value_id = 23
animal_attrib_value_name = 'I am some value that needs to be changed.'
Zur Laufzeit habe ich nur dieanimal_id
(458). Ich muss das entsprechende nachschlagenanimal_attrib_value
(23) und ändere seineanimal_attrib_value_name
zu'Some new value'
, alles innerhalb einer einzelnen UPDATE-Anweisung.