使用自连接更新 SQLite 中的 NULL 值
Using a self join to update NULL values in SQLite
所以我有一个缺少 propertyaddress 值的 table,尽管 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]
此代码抛出错误
如果您的 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;
所以我有一个缺少 propertyaddress 值的 table,尽管 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]
此代码抛出错误
如果您的 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;