How to copy data between PostgreSQL tables

I have two tables: account_company and document_invoice. The account_company table has 2 columns: company_id and company_name. The document_invoice table has the same columns: company_id and company_name. Something happened and delete all the data from the column company name from the invoice_document.

How can I write an SQL query to copy data from the company table of the company to the invoice_document? I use UPDATE and SET, but I don’t know exactly how to do this.

UPDATE document_invoice SET company_name = (SELECT company_name FROM account_company) WHERE document_id.company_name=document_id.account 

enter image description here

+6
source share
1 answer

This should work:

 UPDATE document_invoice t1 SET company_name = t2.company_name FROM account_company t2 WHERE t1.company_id = t2.company_id 
+8
source

Source: https://habr.com/ru/post/914340/


All Articles