我已经得到了帮助进行此查询,但我需要查询只影响非NULL的单元格
我有这个问题
我有一个名为'person'的表,如下所示:
id name country father_id 52 bob NULL 68 68 joe Maui 72 53 mia NULL 68 51 robbie NULL 68
我现在想直接在数据库中运行查询以更新所有内容 person.id与他们的father_id具有相同的国家/地区
因此人员表将如下所示:
id name country father_id 52 bob Maui 68 68 joe Maui 72 53 mia Maui 68 51 robbie Maui 68
所以我的问题是如何根据person.id进行更新 father_id的国家/地区
我在下面得到了这个解决方案,但如上所述,我需要更新只影响具有值的单元格而不是NULL单元格...
update person p join
person father
on p.father_id = father.id
set p.country = father.country;
答案 0 :(得分:0)
添加WHERE
子句
UPDATE person AS p
INNER JOIN person AS father
ON p.father_id = father.id
SET p.country = father.country
WHERE p.country IS NULL
AND father.country IS NOT NULL
答案 1 :(得分:0)
UPDATE person AS child
INNER JOIN person AS father ON child.father_id=father.id
SET child.country=father.country
WHERE child.country IS NULL;