How to Update in Two Table?

Asked

Viewed 207 times

0

I’m looking to make a update in two tables, postgres.

I’ve seen some posts using other banks and I believe I’m doing the same, maybe some particularity of the bank or is missing something. Update in two MYSQL tables

I have 3 tables

Tipo -> pkey id
Grupo -> pkey id e fkey(idTipo -> Tipo.id)
Produto -> pkey id e fkey(idGrupo -> Grupo.id)

Ex: int id = 10 ; boolean = false;

I want to use two SQL statements: the first will be a simple update that changes the visibility (Boolean) in the type table.

String sql = "update tipo set visibilidade = false where id = 10";

the second shall change the child groups of type 10 and successively the child products of the child groups of type '10' to 'false'.

I tried so:

UPDATE grupo g
    INNER JOIN produto p on g.id = p.idGrupo
    SET g.visibilidade = false, p.visibilidade = false
    WHERE g.idTipo=10;

ERROR: syntax error at or near "INNER" LINE 1: UPDATE group g INNER JOIN product p on

I tried otherwise, changing only one of the fields...

UPDATE produto
SET visibilidade = true
FROM grupo g INNER JOIN produto p on g.id = p.idGrupo
WHERE g.idTipo=10

The code runs...but changes all bank records... As long as you select...

select * 
FROM grupo g INNER JOIN produto p on g.id = p.idGrupo
WHERE g.idTipo=10;

works perfectly.

  • Try using ON instead of INNER JOIN.

  • @Diêgocorreiadeandrade be clearer by kindness. I’ve been using on.... wherever put on?

  • See the correct UPDATE syntax in the manual: https://www.postgresql.org/docs/current/sql-update.html. Instead of this JOIN, use the FROM clause. Make two updates, one for each table, within a transaction.

  • I saw the manual but still do not know how to do...

  • could you post the creation commands of the tables involved? So I think I can mount an example in sqlfiddle.

1 answer

0

Try to use your syntax this way:

UPDATE grupo g
SET g.visibilidade = false, p.visibilidade = false
FROM 'sua tabela'
INNER JOIN produto p on g.id = p.idGrupo 
WHERE g.idTipo=10;
  • ERROR: syntax error at or near "INNER" LINE 3: INNER JOIN product p on g.id = p.idGroup

  • Let’s see, we have to put a FROM, I’ll correct my answer!

  • 'your table' which one?

  • If you do not funfar the Product tries the group... The logic is this, as if it were a select

  • Take a look here http://sqlfiddle.com/#! 9/7d065b/1

  • Note the detail that does not work in Postgresql.

  • Yes, no postgres does not work.

Show 2 more comments

Browser other questions tagged

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