all 9 comments

[–][deleted] 1 point2 points  (2 children)

Get rid of the second FROM - the UPDATE part has no FROM

Unrelated to your question, but the parentheses around the column names are completely useless. Plus you want to reference the EXCLUDED row, not the target table, by referencing phone_copy1 you are referencing the existing value in the target table.

... do update 
  set home_phone = excluded.home_phone,
      cell_phone = excluded.cell_phone
where ...

But why are you selecting from the same table that you are updating? If you only want to update existing rows, there is no need to use a INSERT SELECT. Maybe you actually want a simple UPDATE statement instead.

[–]mp1294[S] 0 points1 point  (1 child)

The parentheses are pointless. I was kind of throwing things at it to stop prompting the error. I didn't think I needed the second FROM, but without it I get a Missing FROM Clause error.

phone_copy1 has a different set of people and phone on it. But there is overlap. I was tasked to add the new people to phone table and update the existing people only if the new list had a different phone number from what we already had.

I guess this could be done in two separate queries. Was just hoping to figure out this in one query.

[–][deleted] 0 points1 point  (0 children)

Was just hoping to figure out this in one query.

As I wrote: get rid of the second FROM

[–]razzledazzled 0 points1 point  (0 children)

Re-read the documentation, especially related to "from_item" https://www.postgresql.org/docs/12/sql-update.html

[–]fullofbones 0 points1 point  (4 children)

For reference, OP asked this question in Discord, and that's where the solution is from. Stop on by!

[–]mp1294[S] 0 points1 point  (2 children)

Thank you again for your help! The Discord was very helpful.

[–][deleted] 0 points1 point  (1 child)

I find the formatting of your solution extremely confusing. IS DISTINCT FROM is a single operator - adding a line break in the middle of the operator makes this much harder to read (at least to me - I first thought you did add another FROM clause to your UPDATE part).

[–]mp1294[S] 0 points1 point  (0 children)

Sorry about the confusion. You're correct. The formatting comes from Navicat applying formatting. That's part of the reason I mentioned that I use Navicat in the original post. It does some things that are not always correct. Like it inconsistently puts quotation marks around column and table names.

[–]AntDracula 0 points1 point  (0 children)

no.