SQLite:如何从其他表更新列

时间:2017-07-11 00:45:45

标签: sql sqlite

我有tableA

ID | Zipcode |  State  
1  | 76020   |  NULL  
2  | 40508   |  NULL 
3  | 90040   |  NULL 

和tableB涵盖了美国的所有邮政编码和州

Zipcode |  State

00210   |  NH
00211   |  NH
00212   |  NH

如何通过匹配邮政编码从tableB更新tableA中的NULL值?

3 个答案:

答案 0 :(得分:0)

您可以使用相关子查询:

update a
    set state = (select b.state from b where b.zipcode = a.zipcode);

答案 1 :(得分:0)

您可以使用Update with Join,如下所示 -

function tableprime = expandtable(temptable, field)

    tableprime = temptable;
    count = 1;

    for i = 1:height(temptable) 
        C = temptable{i, field};
        if length(C{:}) > 1 
            temp = tableprime(1:count-1,:);
            for j = 1:length(C{:})
                temp  = [temp; tableprime(count,1), table(C{1}(j), 'VariableNames', {temptable.Properties.VariableNames{field}}), tableprime(count, 3:17)];
            end
            tableprime = [temp; tableprime(count+1:height(tableprime),:)];
        end
        count = count + length(C{:});
    end
end

答案 2 :(得分:0)

UPDATE a
SET a.State = b.State
FROM tableA a
    INNER JOIN tableB b ON b.Zipcode = a.Zipcode
WHERE a.State IS NULL