关于sql:使用子查询更新postgres中的表行

updating table rows in postgres using subquery

使用postgres 8.4,我的目标是更新现有表:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
CREATE TABLE public.dummy
(
  address_id SERIAL,
  addr1 CHARACTER(40),
  addr2 CHARACTER(40),
  city CHARACTER(25),
  state CHARACTER(2),
  zip CHARACTER(5),
  customer BOOLEAN,
  supplier BOOLEAN,
  partner BOOLEAN

)
WITH (
  OIDS=FALSE
);

最初我使用insert语句测试了我的查询:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
INSERT INTO address customer,supplier,partner
SELECT  
    CASE WHEN cust.addr1 IS NOT NULL THEN TRUE ELSE FALSE END customer,
    CASE WHEN suppl.addr1 IS NOT NULL THEN TRUE ELSE FALSE END supplier,
    CASE WHEN partn.addr1 IS NOT NULL THEN TRUE ELSE FALSE END partner
FROM (
    SELECT *
        FROM address) pa
    LEFT OUTER JOIN cust_original 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 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 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

是新手我没有转换为更新语句即,用select语句返回的值更新现有行。
任何帮助都非常感谢。


Postgres允许:

1
2
3
4
5
6
7
UPDATE dummy
SET customer=subquery.customer,
    address=subquery.address,
    partn=subquery.partn
FROM (SELECT address_id, customer, address, partn
      FROM  /* big hairy SQL */ ...) AS subquery
WHERE dummy.address_id=subquery.address_id;

此语法不是标准SQL,但对于此类查询比标准SQL更方便。 我相信Oracle(至少)接受类似的东西。


你是在UPDATE FROM语法之后。

1
2
3
4
5
6
7
8
9
UPDATE
  TABLE T1  
SET
  column1 = t2.column1
FROM
  TABLE t2
  INNER JOIN TABLE t3 USING (column2)
WHERE
  t1.column2 = t2.column2;

参考

  • 此处的代码示例:UPDATE FROM子句中的GROUP BY
  • 和这里
  • 形式语法规范


如果使用连接没有性能提升,那么我更喜欢通用表表达式(CTE)以提高可读性:

1
2
3
4
5
6
7
8
9
10
WITH subquery AS (
    SELECT address_id, customer, address, partn
    FROM  /* big hairy SQL */ ...
)
UPDATE dummy
SET customer = subquery.customer,
    address  = subquery.address,
    partn    = subquery.partn
FROM subquery
WHERE dummy.address_id = subquery.address_id;

恕我直言更现代。


1
2
3
4
5
6
UPDATE json_source_tabcol AS d
SET isnullable = a.is_Nullable
FROM information_schema.columns AS a
WHERE a.table_name =d.table_name
AND a.table_schema = d.table_schema
AND a.column_name = d.column_name;