I am trying to run some update queries on Postgres where tables are joined on common field. This is run with SQL Server and the update count is 1 which is expected whereas postgres the update count is 3. It seems that postgres does not perform implicit join when the destination table is the same name as the source table involved in the join. The script could be more descript than what is being said and here it is:
drop table test;
drop table test2;
create table test(col1 int, col2 varchar(10));
insert into test values(1, 'aaa');
insert into test values(2, 'bbb');
insert into test values(3, 'ccc');
create table test2(col1 int);
insert into test2 values(2);
select * from test;
select * from test2;
// Select join = rowcount 1
select count(*) from test t2, test2 t3
where t2.col1 = t3.col1;
// SQL Server update = 1; postgres =3
update test set col2 = 'changed'
from test t2, test2 t3
where t2.col1 = t3.col1;
The above query can be simplified with:
update test set col2 = 'changed'
from test2 where test.col1 = test2.col1;
but that is not my intention as the join clause might involve some more join statements. The desired intention was to run a query like this:
UPDATE IDMAP_CHILD_JOBID SET RESTORESUCCESS = IDMAP_TABLE_JOBID.RESTORESUCCESS, RESTOREERRMSG = IDMAP_TABLE_JOBID.RESTOREERRMSG
FROM CHILD, IDMAP_TABLE_JOBID
WHERE CHILD.ID = IDMAP_CHILD_JOBID.OLDID AND CHILD.FK1 = IDMAP_TABLE_JOBID.OLDID
AND IDMAP_TABLE_JOBID.RESTORESUCCESS = $FALSE
Postgres complains with table specified more than once if IDMAP_CHILD_JOBID is the same as IDMAP_TABLE_JOBID. How can this be rewritten? My application is supposed to generate an update statement where this is unified query is supposed to be run whereas the behaviour is different. It is obvious that join is performed on select while it is not under update.
IDMAP_TABLE_JOBIDfrom theFROMpartsome more join statementsthere is no such thing as a JOIN statement. JOIN is a binary operator, its operands are two table(-expression)s and its result is a table-expression.