Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting distinct values from two tables

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!

like image 762
Tucker Avatar asked Mar 17 '26 18:03

Tucker


2 Answers

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)
like image 178
Richard Pianka Avatar answered Mar 20 '26 08:03

Richard Pianka


You can use

UNION ALL

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

like image 36
Adrian Serafin Avatar answered Mar 20 '26 08:03

Adrian Serafin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!