Selecting distinct values from two tables

时光总嘲笑我的痴心妄想 提交于 2019-12-11 04:06:30

问题


I have two rather large databases (+1 million rows each). Both tables have the same structure.

How can I check if each value in a column is unique across both tables?

Is there a
SELECT COUNT(DISTINCTcol) FROM tbl
type of query that will consider BOTH tables?

Thanks!


回答1:


You can UNION two full sets in a subquery and then select DISTINCT col from that.

Something like:

SELECT DISTINCT col FROM (SELECT * FROM tbl1 UNION ALL SELECT * FROM tbl2)



回答2:


You can use

UNION ALL

statement. It doesn't remove duplicate rows so you can see if there are any duplicates.




回答3:


Here is my initial thought in pseudocode.

select tableOne.distinctcol
from
(select distinct col as distinctcol from tb1) as tableOne
(select distinct col as distinctcol from tb2) as tableTwo
where tableOne.distinctcol = tableTwo.distinctcol

Basic get a distinct list of values from each table, join them on that column.



来源:https://stackoverflow.com/questions/5158600/selecting-distinct-values-from-two-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!