How to return values from postgres update using a join

85 Views Asked by At

I'm working with postgres and I want to write a single query that both updates a boolean value and when it's finished shows the updated value plus some additional columns. currently, I'm using the returning statement, but some of the columns I would like to display come from a join with another table (actores)

This is my current query:

update entradas set vendida=false where ticket_id - 1134 = 101 returning ticket_id-1134 as numero_entrada, vendida, name, fecha;

name and fecha are fields in the table actores, so how could I do something resembling:

from entradas join actores on entradas.user_id = actores.user_id

Thanks in advance!

1

There are 1 best solutions below

5
On

Prefix the table name to the column name, so if you have data with the same column names from table_A and table_B, or even from a named query, you can do:

table_A.column_name, table_B.column_name, query.column_name to explicitly reference the columns you want.

For the join, here is the syntax you want to look at:


UPDATE mytable
    SET vendida = false
FROM entradas
  JOIN actores ON entradas.user_id = actores.user_id
WHERE (ticket_id - 1134) = 101
RETURNING (,,,)
;