作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要返回两个表之间的差异。
创建临时表
CREATE TEMP TABLE first(
zoom smallint NOT NULL,
x integer NOT NULL,
y integer NOT NULL
);
CREATE TEMP TABLE second(
zoom smallint NOT NULL,
x integer NOT NULL,
y integer NOT NULL
);
插入数据
INSERT INTO first(zoom,x,y) VALUES(5,2,25);
INSERT INTO first(zoom,x,y) VALUES(5,4,45);
INSERT INTO first(zoom,x,y) VALUES(5,7,34);
INSERT INTO first(zoom,x,y) VALUES(5,45,40);
INSERT INTO first(zoom,x,y) VALUES(5,72,63);
INSERT INTO second(zoom,x,y) VALUES(5,2,25);
INSERT INTO second(zoom,x,y) VALUES(5,4,45);
INSERT INTO second(zoom,x,y) VALUES(5,7,34);
想要的结果:
In table first there are extra rows:
5,45,40
5,72,63
编辑
很抱歉,但我现在发现我的原始数据比我提供的样本复杂得多。所以在原始数据中,table first 包含 900 行,table second 包含 935 行。我假设每个表中的行都是不同的,但是我现在不确定,所以我想在查询中包含这个条件。我假设查询将返回 35 行作为差异,因为我非常确信除了这 35 行之外,所有缩放/x/y 都是相同的。但是,现在可能是这样。所以基本上我需要知道的是两个表之间的区别是什么,无论哪种方法是解决它的最佳方法。
我能得到这样的东西吗:
zoom | x | y | first |second
------+----+--- +-------+------
5 | 45 | 40 | yes | no |
顺序是第一个是,第二个不是
zoom | x | y | first |second
------+----+--- +-------+------
5 | 45 | 40 | yes | no |
5 | 45 | 40 | yes | no |
5 | 45 | 40 | yes | no |
先否,后是
zoom | x | y | first |second
------+----+--- +-------+------
5 | 45 | 40 | no | yes |
5 | 45 | 40 | no | yes |
5 | 45 | 40 | no | yes |
最佳答案
您可以使用 EXCEPT
select zoom,x,y from first
except
select zoom,x,y from second
或者这里遗漏了什么
如果您想要两个表中的不匹配记录,那么
select * from
(
select zoom,x,y from first
except
select zoom,x,y from second
) a
union all
select * from
(
select zoom,x,y from second
except
select zoom,x,y from first
) b
关于sql - 如何进行查询以返回 PostgreSQL 中两个表之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40221733/
我是一名优秀的程序员,十分优秀!