Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I copy data between tables PostgreSQL

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

How can I write SQL query to copy data from account company table to document_invoice? I have been using UPDATE and SET but I don't know exactly how.

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

enter image description here

like image 876
ssuperczynski Avatar asked Apr 27 '12 13:04

ssuperczynski


1 Answers

This should work:

UPDATE document_invoice t1 
SET company_name = t2.company_name
FROM account_company t2 
WHERE t1.company_id = t2.company_id
like image 106
Lamak Avatar answered Nov 20 '22 01:11

Lamak