Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UPDATE statement with multiple joins in PostgreSQL

I'm trying to update a table called incode_warrants and set the warn_docket_no to the viol_docket_no from the incode_violations table.

I have the following SQL query in Postgres 9.3, but when it fires I get the following error:

Error : ERROR:  relation "iw" does not exist
LINE 1: update iw

I'm more of an Active Record person so my raw SQL skills are seriously lacking. I was wondering if anyone could help point me in the right direction on how to get this query right.

update iw
set iw.warn_docket_no = iv.viol_docket_no
from incode_warrants as iw
INNER JOIN incode_warrantvs as iwvs
on iw.warn_rid = iwvs.warnv_rid
INNER JOIN incode_violations as iv
ON iv.viol_citation_no = iwvs.warnv_citation_no and iv.viol_viol_no = iwvs.warnv_viol_no
like image 456
nulltek Avatar asked Sep 03 '15 22:09

nulltek


People also ask

Are joins allowed in UPDATE statement?

An UPDATE statement can include JOIN operations.

Can we use join in UPDATE query in PostgreSQL?

Introduction to the PostgreSQL UPDATE join syntaxTo join to another table in the UPDATE statement, you specify the joined table in the FROM clause and provide the join condition in the WHERE clause. The FROM clause must appear immediately after the SET clause.

How do you UPDATE multiple tables in a single query?

1 Answer. It's not possible to update multiple tables in one statement, however, you can use the transaction to make sure that two UPDATE statements must be treated atomically. You can also batch them to avoid a round trip like this.

Can we UPDATE multiple columns in a single UPDATE statement?

We can update multiple columns by specifying multiple columns after the SET command in the UPDATE statement. The UPDATE statement is always followed by the SET command, it specifies the column where the update is required.


3 Answers

The same as valid UPDATE statement in Postgres:

UPDATE incode_warrants iw
SET    warn_docket_no = iv.viol_docket_no
FROM   incode_warrantvs  iwvs
JOIN   incode_violations iv ON iv.viol_citation_no = iwvs.warnv_citation_no
                           AND iv.viol_viol_no = iwvs.warnv_viol_no
WHERE  iw.warn_rid = iwvs.warnv_rid;
-- AND iw.warn_docket_no IS DISTINCT FROM iv.viol_docket_no -- see below

You cannot just use a table alias in the FROM clause as target table in the UPDATE clause. The (one!) table to be updated comes right after UPDATE keyword (if we ignore a possible ONLY keyword in between). You can add an alias there if you want. That's the immediate cause of your error message, but there's more.

The column to be updated is always from the one table to be updated and cannot be table-qualified.

You don't need to repeat the target table in the FROM clause - except for special cases like this:

  • PostgreSQL: update with left outer self join ignored

This optional addition can avoid pointless cost by suppressing updates that do not change anything:

AND iw.warn_docket_no IS DISTINCT FROM iv.viol_docket_no

See:

  • How do I (or can I) SELECT DISTINCT on multiple columns?

More in the excellent manual on UPDATE.

like image 169
Erwin Brandstetter Avatar answered Oct 28 '22 04:10

Erwin Brandstetter


Your query should look like this:

UPDATE incode_warrants
SET warn_docket_no = incode_violations.viol_docket_no
FROM incode_violations
WHERE incode_violations.viol_citation_no = incode_warrants.warnv_citation_no
AND incode_violations.viol_viol_no = incode_warrants.warnv_viol_no;

You don't need any other join. With this query you just update a column in one table with values from a column from another table. Of course, it updates only when WHERE condition is true.

like image 26
Walerian Sobczak Avatar answered Oct 28 '22 04:10

Walerian Sobczak


UPDATE incode_warrants iw
     SET warn_docket_no = iv.viol_docket_no
FROM incode_warrantvs AS iwvs, incode_violations AS iv 
WHERE iv.viol_citation_no = iwvs.warnv_citation_no AND
      iv.viol_viol_no = iwvs.warnv_viol_no AND
      iw.warn_rid = iwvs.warnv_rid;
like image 4
user2035082 Avatar answered Oct 28 '22 03:10

user2035082