PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 24, 2022

[FIXED] How to UPDATE a column from a self join table in PostgreSQL?

 October 24, 2022     postgresql, self-join, sql-update     No comments   

Issue

I need to replace a column by a column from a self join table but I got an error. What should I do? Thanks!

UPDATE a
SET propertyaddress=COALESCE(a.propertyaddress,b.propertyaddress)
FROM Housing a
JOIN Housing b
  ON a.ParcelID=b.ParcelID
  AND a.uniqueid!=b.uniqueid
WHERE a.propertyaddress IS NULL); 

ERROR: relation "a" does not exist LINE 32: UPDATE a ^


Solution

You were using the Microsoft-Syntax for UPDATE. (and you don't need the COALESCE(), since a.propertyaddress is always NULL)

Postgres documentation for UPDATE


UPDATE Housing a
SET propertyaddress = b.propertyaddress
FROM Housing b
WHERE a.ParcelID = b.ParcelID
  AND a.uniqueid <> b.uniqueid
  AND a.propertyaddress IS NULL
  ; 


Answered By - wildplasser
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing