【问题标题】:How to copy data from one column to another?如何将数据从一列复制到另一列?
【发布时间】:2021-11-28 16:23:21
【问题描述】:

我正在尝试在缺少值的列中添加属性地址。

我使用下面的方法来识别具有相应属性地址的常见宗地 ID,因为相同的宗地 ID 也具有相同的 PropertyAddress。

select n.UniqueID, n.ParcelID, n.PropertyAddress, n2.UniqueID, n2.ParcelID, n2.PropertyAddress, IFNULL(n.PropertyAddress,n2.PropertyAddress) 
from Nashnew n 
join Nashnew n2 
on n.ParcelID = n2.ParcelID 
where n2.PropertyAddress =''
and n.UniqueID != n2.UniqueID 

我得到这个结果:

现在我想使用以下方法将 IFNULL(n.PropertyAddress,n2.PropertyAddress) 列中的数据添加到缺少的 PropertyAddress 单元格中:

UPDATE Nashnew 
set propertyAddress = IFNULL(n.PropertyAddress,n2.PropertyAddress)
from Nashnew n 
join Nashnew n2 
on n.ParcelID = n2.ParcelID 
and n.UniqueID != n2.UniqueID 
where n2.PropertyAddress =''

但是,我得到了这个结果,其中所有行的所有 PropertyAddress 都是相同的。

如何将正确的 PropertyAddress 添加到

【问题讨论】:

  • 在第二次查询中加入时,为什么加入的唯一 ID 不等于唯一 ID?
  • 否则,当我加入表格时,会有重复的行。

标签: sql sqlite join sql-update sql-null


【解决方案1】:

使用UPDATE 语句中的代码,您实际上加入了两次表Nashnew
您还应该检查Nashnew 的更新副本的PropertyAddress 列是否为null 或为空:

改成这样:

UPDATE Nashnew AS n
SET propertyAddress = n2.PropertyAddress
FROM Nashnew AS n2 
WHERE n.ParcelID = n2.ParcelID 
  AND n.UniqueID <> n2.UniqueID 
  AND COALESCE(n.PropertyAddress, '') = ''
  AND COALESCE(n2.PropertyAddress, '') <> '';

【讨论】:

    猜你喜欢
    • 2016-03-15
    • 1970-01-01
    • 1970-01-01
    • 2018-12-27
    • 2011-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    相关资源
    最近更新 更多