What is the syntax for UPDATE from one table to another?

Started by pwabout 22 years ago4 messagesgeneral
Jump to latest
#1pw
p.willis@telus.net

Hello,

What is the proper syntax for updating a column inone table from
a column in another?

I have tried this:

UPDATE destination_table FROM source_table SET
destination_table.column_one = source_table.column_b WHERE constraint;

This fails, so I must have the syntax incorrect.

Thanks for any help,

Pete

#2Jeff
threshar@torgo.978.org
In reply to: pw (#1)
Re: What is the syntax for UPDATE from one table to another?

On Mar 12, 2004, at 12:40 PM, pw wrote:

Hello,

What is the proper syntax for updating a column inone table from
a column in another?

I have tried this:

UPDATE destination_table FROM source_table SET
destination_table.column_one = source_table.column_b WHERE constraint;

update destination_table set column_a = (select column_b from
source_table where blah blah blah ) where blah blah blah

You can reference the values in destination_table inside the select.
--
Jeff Trout <jeff@jefftrout.com>
http://www.jefftrout.com/
http://www.stuarthamm.net/

#3Bruno Wolff III
bruno@wolff.to
In reply to: pw (#1)
Re: What is the syntax for UPDATE from one table to another?

On Fri, Mar 12, 2004 at 09:40:13 -0800,
pw <p.willis@telus.net> wrote:

Hello,

What is the proper syntax for updating a column inone table from
a column in another?

I have tried this:

UPDATE destination_table FROM source_table SET
destination_table.column_one = source_table.column_b WHERE constraint;

This fails, so I must have the syntax incorrect.

If you check the documentation you will see that SET is supposed to be before
FROM.

#4pw
p.willis@telus.net
In reply to: pw (#1)
Re: (THE_ANSWER) What is the syntax for UPDATE from one

Thanks to everyone who helped.
I found the solution just by tooling with the SQL syntax.

FYI, the correct syntax is:

UPDATE destination_table SET dest_column=A FROM
(SELECT src_column as A, src_link_col FROM src_table) AS J
WHERE src_link_col=dst_link_col;

Pete

pw wrote:

Show quoted text

Hello,

What is the proper syntax for updating a column inone table from
a column in another?

I have tried this:

UPDATE destination_table FROM source_table SET
destination_table.column_one = source_table.column_b WHERE constraint;

This fails, so I must have the syntax incorrect.

Thanks for any help,

Pete

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html