Reputation: 89
I have two tables tbl_user1
and tbl_user2
both are field name are same but there is no relation between that tables now I want to find total referred count from both table
for example...
tbl_user1
-----------------------
UID | referenceBy | firstName | lastName | emailAddress
----------------------------------------------------------------------------
1 | NULL | aa1 | ab1 | [email protected]
2 | [email protected] | aa2 | ab2 | [email protected]
3 | NULL | aa3 | ab3 | [email protected]
4 | [email protected] | aa4 | ab4 | [email protected]
5 | [email protected] | aa5 | ab5 | [email protected]
6 | [email protected] | aa6 | ab6 | [email protected]
7 | [email protected] | aa7 | ab7 | [email protected]
8 | [email protected] | aa8 | ab8 | [email protected]
9 | [email protected] | aa9 | ab9 | [email protected]
and second one table is somthing like...
tbl_user2
-----------------------
UID | referenceBy | firstName | lastName | emailAddress
----------------------------------------------------------------------------
1 | NULL | bb1 | bc1 | [email protected]
2 | [email protected] | bb2 | bc2 | [email protected]
3 | NULL | bb3 | bc3 | [email protected]
4 | [email protected] | bb4 | bc4 | [email protected]
5 | [email protected] | bb5 | bc5 | [email protected]
6 | [email protected] | bb6 | bc6 | [email protected]
7 | [email protected] | bb7 | bc7 | [email protected]
8 | [email protected] | bb8 | bc8 | [email protected]
9 | [email protected] | bb9 | bc9 | [email protected]
now, as you can see there is no relation between these two tables and I want result like following..
MAIN_RESULT_THAT_I_WANT
-----------------------
referenceEmail | referenceEmailCount
----------------------------------------------------------------------------
[email protected] | 1
[email protected] | 3
[email protected] | 1
[email protected] | 0
[email protected] | 0
[email protected] | 0
[email protected] | 0
[email protected] | 0
[email protected] | 0
[email protected] | 3
[email protected] | 2
[email protected] | 3
[email protected] | 0
[email protected] | 1
[email protected] | 0
[email protected] | 0
[email protected] | 0
[email protected] | 0
here in result all emailAddress of all user and total of how many user(s) registered by that particular emailAddress.
Upvotes: 0
Views: 45
Reputation: 12391
Since the schema is same for 2 tables so you can perform union
to get combined results and can perform an outer query to get the total count.
select referenceEmail, count(*) as referenceEmailCount from (
select * from table1
union all
select * from table2
) as alias
group by alias.referenceEmail
Upvotes: 0
Reputation: 1946
I am guessing that the result you want is just copy and pasted since it seems inaccurate. Like HoneyBadger says it is strange that aa6 is missing and still in the result, that indicates you have another list you are not telling us about? Or you just write the result in notepad...
If you just want a list of emails and count this will work:
select referenceBy, count(1) as referenceEmailCount from (
select referenceBy from tbl_user1
union all
select referenceBy from tbl_user2
) as t
group by referenceBy
Otherwise give us more info if this is not what you need.
Upvotes: 2