E
E
EVOSandru62015-10-02 11:03:37
PostgreSQL
EVOSandru6, 2015-10-02 11:03:37

How to pick up identical fields in a one-to-many relationship in postgres?

Good afternoon,
There are 2 tables - m_hotels (hotels), mc_rooms (hotel rooms)
The connection is as follows: mc_rooms.hotel_id == m_hotels.id
I need to assign m_hotels.firm_id values ​​to the c_rooms.firm_id fields.
I tried this:

update mc_rooms set mc_rooms.firm_id = m_hotels.firm_id where  mc_rooms.hotel_id = m_hotels.id;

BUT got an error:
ERROR: missing FROM-clause entry for table "m_hotels"
LINE 3: update mc_rooms set mc_rooms.firm_id = m_hotels.firm_id wher...
^
********** Error ***** *****
ERROR: missing FROM-clause entry for table "m_hotels"
SQL-state: 42P01
Symbol: 85

How to do it right?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
EVOSandru6, 2015-10-02
@EVOSandru6

UPDATE mc_rooms
SET firm_id=m_hotels.firm_id
from m_hotels
WHERE mc_rooms.hotel_id=m_hotels.id;

D
Dmitry Kovalsky, 2015-10-02
@dmitryKovalskiy

UPDATE t
SET col1 = value
FROM table1 as t
--Далее JOIN если надо и т.д.
WHERE и т.д.

You from forgot what you were clearly told

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question