如何更新表中与另一个表不同的行?

时间:2015-10-29 00:09:49

标签: sql postgresql

我有两张桌子

boxes
id, name, width, height, is_changed
1, ben, 10, 12, false
2, carol, 13, 10, false
3, david, 2, 33, false
4, ellen, 9, 17, false

boxes_new
id, name, width, height
1, ben, 1000, 12
2, xavier, 13, 10
3, david, 2, 33
4, ellen, 9, 17

请注意,box_new

中的方框1和2不同

我想做点什么

UPDATE boxes SET is_changed=true WHERE boxes.name != boxes_new.is_name OR boxes.width != boxes_new.width

此外,如果出于性能原因,这些表将有数十万行。

3 个答案:

答案 0 :(得分:1)

假设id匹配两个框:

update boxes b
    set is_changed = true
    from boxes_new bn
    where bn.id = b.id and
          (b.name <> bn.name or
           b.width <> bn.width or
           b.height <> bn.height
          );

答案 1 :(得分:0)

I think some nested query like this, could help,

UPDATE框SET is_changed = true WHERE id NOT IN(SELECT b.id FROM boxes b INNER JOIN boxes_new bn ON(bname = bn.name AND b.width = bn.width));

答案 2 :(得分:0)

如果可以使用NULL值,为了涵盖所有可能性,您必须使用NULL-safe comparison

public class Parent {
    private var childClass:ClassName;

    private function bindSignal() {
        childClass.signal.add(signalListener);
    }

    private function signalListener(classReference:ClassName, foo:String) {
        /* do your stuff with classReference */
    }
}