【问题标题】:Using a self join to update NULL values in SQLite使用自连接更新 SQLite 中的 NULL 值
【发布时间】:2022-01-01 10:57:38
【问题描述】:

所以我有一个缺少 propertyaddress 值的表,尽管 ParcelID 列可用于标识该值。如何在 sqlite 中执行自联接以使用其真实的 propoertyaddress 值更新 NULL 值?

我尝试了以下代码

SELECT a.ParcelID, a.PropertyAddress, b.ParcelID, b.PropertyAddress, IFNULL(a.PropertyAddress, b.PropertyAddress)
FROM housing_data a
JOIN housing_data b
    ON a.ParcelID = b.ParcelID
    AND a.[UniqueID] <> b.[UniqueID]
WHERE a.PropertyAddress is null;


UPDATE a
SET PropertyAddress = IFNULL(a.PropertyAddress, b.PropertyAddress)
FROM housing_data  a
WHERE a.PropertyAddress IS NULL;
JOIN housing_data  b
    ON a.ParcelID = b.ParcelID
    AND a.[UniqueID] <> b.[UniqueID]

此代码引发错误

【问题讨论】:

  • WHERE 移到JOIN 下方UPDATE

标签: sql sqlite join sql-update


【解决方案1】:

如果您的 SQLite 版本是 3.33.0+,您可以使用 UPDATE FROM 语法:

UPDATE housing_data AS d1
SET PropertyAddress = d2.PropertyAddress
FROM housing_data AS d2
WHERE d2.ParcelID = d1.ParcelID AND d1.PropertyAddress IS NULL AND d2.PropertyAddress IS NOT NULL;

【讨论】:

    猜你喜欢
    • 2013-09-07
    • 1970-01-01
    • 2020-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多