如何比较Postgres中的两个表 [英] How to compare two tables in postgres
问题描述
我想比较来自两个不同查询的两个列值。有人可以建议比较Postgres中两列的查询吗?
I want compare two column values which come from two different queries. Can anyone suggest a query which compares two columns in Postgres?
推荐答案
最容易理解,但不一定最快-大概是这样的。 (但是您可能会通过比较来表示其他含义。)
Well, the easiest to understand--but not necessarily the fastest--is probably something like this. (But you might mean something else by "compare".)
-- Values in column1 that aren't in column2.
SELECT column1 FROM query1
WHERE column1 NOT IN (SELECT column2 FROM query2);
-- Values in column2 that aren't in column1.
SELECT column2 FROM query2
WHERE column2 NOT IN (SELECT column1 FROM query1);
-- Values common to both column1 and column2
SELECT q1.column1 FROM query1 q1
INNER JOIN query2 q2 ON (q1.column1 = q2.column2);
您也可以在单个语句中进行此操作以进行直观比较。 FULL OUTER JOIN
返回两列中的所有值,并且在同一行中具有匹配的值,而 NULL
其中一个
You can also do this in a single statement to give you a visual comparison. A FULL OUTER JOIN
returns all the values in both columns, with matching values in the same row, and NULL
where one column is missing a value that's in the other column.
SELECT q1.column1, q2.column2 FROM query1 q1
FULL OUTER JOIN query2 q2 ON (q1.column1 = q2.column2);
这篇关于如何比较Postgres中的两个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!