How do I update with a Join?

Asked

Viewed 19,788 times

7

Hello, I’m new to the forum and I need some help with SGBD Postgresql.

My doubt 'and the following, in the following code used in DBMS Mysql the execution happens perfectly, by'em in Postgresql shows an error.

Mysql code:

 UPDATE userT
    INNER JOIN empresa ON userT.idEmp = empresa.idEmp
SET userT.telefone = '15253485',
    empresa.cargaTrab = 12
WHERE idUser = 1;

The above mentioned code performs perfectly in Mysql but in Postgresql appears the following error:

ERROR: syntax error at or near "INNER".

Could someone help me?

  • That doesn’t work in postgres

4 answers

11


Postgres does not support ansi-92 syntax of joins in the update as Mysql in your query should be done using the ansi-86 syntax which is the one where the merge is done in the clasp WHERE

The consultation should look like this:

 UPDATE userT SET
    userT.telefone = '15253485',
    empresa.cargaTrab = 12
 FROM empresa
    WHERE userT.idEmp = empresa.idEmp
    AND idUser = 1;
  • Hi, is it all jewels? I want to update both userT table phone and company table carTrab, so just have comparison of the load worked with idUser.

  • @Andréfrança edits the answers see if this is it.

3

with t as (
  -- Any generic query which returns rowid and corresponding calculated values
  select t1.id as rowid, f(t2, t2) as calculatedvalue
  from table1 as t1
  join table2 as t2 on t2.referenceid = t1.id
)
update t1
set value = t.calculatedvalue
from t
where id = t.rowid
  • 3

    if you can [Dit] and beyond the code give a little explanation, values a bit your post

  • 1

    The use of the term WITH is perfect for making the UPDATE in which it is necessary to JOIN multi-table.

2

I do as follows via select on the condition I need.

update ESTOQUE
set QTDE=100
where CODMARCA in (select CODIGO from marca where nome = CONDIÇÃO)

0

Hello I always do it this way, ride the sql for instruction select Example:

SELECT
       c.nome,
       tc.descricao
from cliente c 
left join tipo_cliente tc on tc.id_tipo_cliente  = c.id_tipo_cliente
    where
tc.id_tipo_cliente=13;

Warning: The following operation is not supported by Postgresql(Update Syntax)

Based on select:

update cliente c
    left join tipo_cliente tc on tc.id_tipo_cliente  = c.id_tipo_cliente
set tc.descricao='TESTE'
where tc.id_tipo_cliente=13;

With the select done and working, copy and delete the from select information back. Add update and after joins and before the where the set.

  • In Postgresql which is the object of this question your code UPDATE gives the following error: Query Error: error: syntax error at or near "left" because Postgresql does not support join within update.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.