We're working on migrating data from MySQL to PostgreSQL.
Sample table structure:
Table "public.model"
Column | Type | Modifiers
--------------------+-----------------------------+-----------
model_id | integer | not null
last_purchase_time | timestamp without time zone |
Indexes:
"model_pkey" PRIMARY KEY, btree (model_id)
Referenced by:
TABLE "items" CONSTRAINT "items_model_id_fkey" FOREIGN KEY (model_id) REFERENCES model(model_id)
Table "public.items"
Column | Type | Modifiers
---------------+-----------------------------+-----------
item_id | integer | not null
purchase_time | timestamp without time zone |
model_id | integer |
Indexes:
"items_pkey" PRIMARY KEY, btree (item_id)
Foreign-key constraints:
"items_model_id_fkey" FOREIGN KEY (model_id) REFERENCES model(model_id)
Referenced by:
TABLE "invoice" CONSTRAINT "invoice_item_id_fkey" FOREIGN KEY (item_id) REFERENCES items(item_id)
Table "public.invoice"
Column | Type | Modifiers
--------------+-----------------------------+-----------
inovoice_id | integer | not null
invoice_time | timestamp without time zone |
item_id | integer |
Indexes:
"invoice_pkey" PRIMARY KEY, btree (inovoice_id)
Foreign-key constraints:
"invoice_item_id_fkey" FOREIGN KEY (item_id) REFERENCES items(item_id)
If we've to update columns of single table using join then we can use below query in postgresql.
update model
set last_purchase_time = now()
from items
join invoice
on items.item_id = invoice.item_id
where model.model_id = items.model_id
and invoice.item_id = 2002;
Below is an update query for updating multiple columns in all tables, which works fine in MySQL.
update model
join items
on model.model_id = items.model_id
join invoice
on items.item_id = invoice.item_id
set model.last_purchase_time = now(),
items.purchase_time = now(),
invoice.invoice_time = now()
where invoice.item_id = 2002;
Can someone suggest an equivalent PostgreSQL query for updating multiple columns in different tables using join?
model.last_purchase_time
is calculable & I see no benefit from being able to supply different timestamps forinvoice
&items
. – pozs Dec 12 '16 at 10:56timestamp
across tables, in real case the values will differ. – vjy Dec 12 '16 at 11:11