Benutzer-Werkzeuge

Webseiten-Werkzeuge


edv:db:sql:example:update_ueber_2_tabellen

Update über 2 Tabellen

(Vielen Dank an Axel Sander!)

UPDATE tableB
SET tableB.fieldA = (SELECT tableA.fieldA FROM tableA WHERE tableA.fieldB = tableB.fieldB)

Warning: that sets to null all the TableB.FieldA values when there is no row in TableA with TableA.FieldB equal to TableB.FieldB. Since this is seldom the desired result, you have to write:

UPDATE TableB
SET TableB.FieldA = (SELECT TableA.FieldA FROM TableA WHERE TableA.FieldB = TableB.FieldB)
WHERE TableB.FieldB IN (SELECT TableA.FieldB FROM TableA)

This happens to be a relatively simple case; if you have filter conditions on the SELECT in the SET clause, then you need to repeat those filter conditions in the WHERE clause of the UPDATE.

EOF

edv/db/sql/example/update_ueber_2_tabellen.txt · Zuletzt geändert: 2020/01/11 01:23 von 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki