sql - 如果 col b 的值匹配,则用 col a 的值填充 col a 的 NULL

标签 sql postgresql null duplicates

假设我有 COUNTRY |邮政编码 列。会有很多重复项,但大多数情况下,一些 COUNTRY 值将丢失,而 POSTAL CODE 将出现。如何在 POSTAL CODE 匹配的其他行中用 COUNTRY 的值填写 COUNTRY

之前

COUNTRY | POSTAL CODE
UK      | ABCXYZ
NULL    | ABCXYZ

之后

COUNTRY | POSTAL CODE
UK      | ABCXYZ
UK      | ABCXYZ

最佳答案

我认为下一次更新就是您所需要的:

update table a
set country = b.country
FROM (select DISTINCT country, postal_code from table where country is not null) b
where a.postal_code = b.postal_code
and a.country is not null;

关于sql - 如果 col b 的值匹配,则用 col a 的值填充 col a 的 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56183738/

相关文章:

javascript - 忽略 Jquery 中的 Null 结果

SQL 获取所有超过 30 天的记录

php - Where IN 子句中的条件和条件

PHP/MySQL - 创建每周报告

java - MYSQL 在 TimeRange 中使用 TIME Between

postgresql - 如何使用 perl 插入 tsrange

c# - TryParse 为可空类型

postgresql - Postgres 10 TDS_FDW 扩展 undefined symbol : Float4GetDatum

java - Hibernate3、PostgreSQL 8.3 和 Java 5 的 "No suitable driver"问题

c++ - 空指针的地址是什么?