Ravi
Ravi

Reputation: 57

How to count distinct elements from different tables in mysql?

this is table_1

d1
--------
a  
b  
c  
d  
e  

table_2

d2
--------
a  
b  
d  

table_3

d3
--------
b  
c  

Expected new table which is sum of count of a,b,c,d,e from all tables above

final_table

a   2  
b   3  
c   2  
d   2  
e   1

Upvotes: 0

Views: 32

Answers (1)

John Ruddell
John Ruddell

Reputation: 25842

you can do a simple union of the tables and a count like so

select id, count(id)
from(
  select id from d1
  union all
  select id from d2
  union all
  select id from d3
) t
group by id

SAMPLE FIDDLE

Upvotes: 1

Related Questions