Home >Database >Mysql Tutorial >How to Update PostgreSQL Table Rows Using Subqueries for Conditional Flag Setting?

How to Update PostgreSQL Table Rows Using Subqueries for Conditional Flag Setting?

Patricia Arquette
Patricia ArquetteOriginal
2025-01-03 20:59:40162browse

How to Update PostgreSQL Table Rows Using Subqueries for Conditional Flag Setting?

Updating Table Rows in PostgreSQL Using Subquery

Postgres provides the capability to update existing table rows using values derived from a subquery. This can be highly beneficial for scenarios where data needs to be modified based on another set of data stored within the database.

Problem Statement:

You have a table named "dummy" with various fields representing address details and customer, supplier, and partner flags. The objective is to update the table by setting the customer, supplier, and partner flags to True or False based on whether matching data exists in the "cust_original," "suppl_original," and "partner_original" tables, respectively.

Solution:

To accomplish this using a SQL update statement, you can leverage PostgreSQL's subquery capability. The update statement takes the following syntax:

UPDATE table_name
SET field1 = subquery_result1,
    field2 = subquery_result2,
    ...
FROM (select field1, field2, ...
      from subquery) AS subquery_name
WHERE table_name.id = subquery_name.id;

In the context of your specific problem, the update query would look like this:

UPDATE dummy
SET customer = subquery.customer,
    supplier = subquery.supplier,
    partner = subquery.partner
FROM (SELECT address_id,
             CASE WHEN cust.addr1 IS NOT NULL THEN TRUE ELSE FALSE END AS customer, 
             CASE WHEN suppl.addr1 IS NOT NULL THEN TRUE ELSE FALSE END AS supplier,
             CASE WHEN partn.addr1 IS NOT NULL THEN TRUE ELSE FALSE END AS partner
      FROM dummy AS pa
      LEFT OUTER JOIN cust_original AS cust
        ON (pa.addr1 = cust.addr1 AND pa.addr2 = cust.addr2 AND pa.city = cust.city
            AND pa.state = cust.state AND SUBSTRING(cust.zip, 1, 5) = pa.zip)
      LEFT OUTER JOIN supp_original AS suppl
        ON (pa.addr1 = suppl.addr1 AND pa.addr2 = suppl.addr2 AND pa.city = suppl.city
            AND pa.state = suppl.state AND pa.zip = SUBSTRING(suppl.zip, 1, 5))
      LEFT OUTER JOIN partner_original AS partn
        ON (pa.addr1 = partn.addr1 AND pa.addr2 = partn.addr2 AND pa.city = partn.city
            AND pa.state = partn.state AND pa.zip = SUBSTRING(partn.zip, 1, 5))
      WHERE pa.address_id = address_id) AS subquery
WHERE dummy.address_id = subquery.address_id;

This subquery-based update statement efficiently updates the "dummy" table by setting the customer, supplier, and partner flags based on the matching data in the subquery, which performs the necessary joins and case evaluations.

The above is the detailed content of How to Update PostgreSQL Table Rows Using Subqueries for Conditional Flag Setting?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn