Hi,
You can rewrite the query like this:
UPDATE
TABLE1What is the exact purpose of the query cause it seems a little bit strange.
Greetz,
Geert
Geert Verhoeven
Consultant @ Ausy Belgium
Hi,
It will limit the selection based on the COL_C field. If you give it a try, you 'll see.
Greetz,
Geert
Geert Verhoeven
Consultant @ Ausy Belgium
No, because the JOIN clause works the same as the WHERE clause so there is actually not much difference between them except for the time of filtering (JOIN occurs before the WHERE).
But lets focus on the intention of the query because I think there need to be a better way.
If I get it good, you want to set COL_A and COL_B to the values of table2 with the same value for COL_C and this only if COL_A is not filled in.
What do you want if you have multiple values in table2 for one value in COL_C.
Greetz,
Geert
Geert Verhoeven
Consultant @ Ausy Belgium
Hi,
Here is the code of the setup and an example of the update statement:
/* SETUP
CREATE TABLE TABLE1
(
COL_A VARCHAR(50),
COL_B VARCHAR(50),
COL_C INT
)
INSERT INTO TABLE1 VALUES (null, null, 1)
INSERT INTO TABLE1 VALUES (null, null, 2)
INSERT INTO TABLE1 VALUES ('Red', 'Green', 3)
CREATE TABLE TABLE2
(
COL_A VARCHAR(50),
COL_B VARCHAR(50),
COL_C INT,
COL_D VARCHAR(50)
)
INSERT INTO TABLE2 VALUES ('Blue', 'Pink', 2, 'Mars')
INSERT INTO TABLE2 VALUES ('Blue', 'Pink', 2, 'Jupiter')
INSERT INTO TABLE2 VALUES ('Blue', 'Pink', 2, 'Uranus')
INSERT INTO TABLE2 VALUES ('Orange', 'Brown', 1, 'Saturn')
INSERT INTO TABLE2 VALUES ('Orange', 'Brown', 1, 'Earth')
INSERT INTO TABLE2 VALUES ('Red', 'Green', 3, 'Neptune')
*/
UPDATE
TABLE1If you run this, you will see that 2 records have been updated. A small question, what if only COL_B has nulls It is possible to set this too like this:
UPDATE
TABLE1With the above statement, COL_A and COL_B are only updated if they are null.
Greetz,
Geert
Geert Verhoeven
Consultant @ Ausy Belgium