从两个没有关系的表中计算记录

时间:2017-05-24 11:32:48

标签: mysql sql

我有两个表tbl_user1tbl_user2这两个字段的字段名相同,但现在这些表之间没有任何关系我希望从两个表中找到总引用计数。 ..

tbl_user1
-----------------------
UID   | referenceBy      |  firstName  |   lastName  |  emailAddress
----------------------------------------------------------------------------
1     | NULL             | aa1         | ab1         | aa1@email.com
2     | aa1@email.com    | aa2         | ab2         | aa2@email.com
3     | NULL             | aa3         | ab3         | aa3@email.com
4     | aa2@email.com    | aa4         | ab4         | aa4@email.com
5     | aa2@email.com    | aa5         | ab5         | aa5@email.com
6     | bb1@email.com    | aa6         | ab6         | aa6@email.com
7     | bb2@email.com    | aa7         | ab7         | aa7@email.com
8     | bb3@email.com    | aa8         | ab8         | aa8@email.com
9     | bb3@email.com    | aa9         | ab9         | aa9@email.com

和第二张桌子就像......左右。

tbl_user2
-----------------------
UID   | referenceBy      |  firstName  |   lastName  |  emailAddress
----------------------------------------------------------------------------
1     | NULL             | bb1         | bc1         | bb1@email.com
2     | bb1@email.com    | bb2         | bc2         | bb2@email.com
3     | NULL             | bb3         | bc3         | bb3@email.com
4     | bb3@email.com    | bb4         | bc4         | bb4@email.com
5     | bb2@email.com    | bb5         | bc5         | bb5@email.com
6     | bb1@email.com    | bb6         | bc6         | bb6@email.com
7     | aa2@email.com    | bb7         | bc7         | bb7@email.com
8     | aa3@email.com    | bb8         | bc8         | bb8@email.com
9     | bb5@email.com    | bb9         | bc9         | bb9@email.com

现在,你可以看到这两个表之间没有任何关系,我希望结果如下......

MAIN_RESULT_THAT_I_WANT
-----------------------
referenceEmail   |  referenceEmailCount 
----------------------------------------------------------------------------
aa1@email.com    | 1
aa2@email.com    | 3
aa3@email.com    | 1
aa4@email.com    | 0
aa5@email.com    | 0
aa6@email.com    | 0
aa7@email.com    | 0
aa8@email.com    | 0
aa9@email.com    | 0
bb1@email.com    | 3
bb2@email.com    | 2
bb3@email.com    | 3
bb4@email.com    | 0
bb5@email.com    | 1
bb6@email.com    | 0
bb7@email.com    | 0
bb8@email.com    | 0
bb9@email.com    | 0

这里结果是所有用户的所有emailAddress以及该特定emailAddress注册的用户总数。

2 个答案:

答案 0 :(得分:2)

我猜你想要的结果只是复制和粘贴,因为它似乎不准确。就像HoneyBadger说的那样,aa6缺失并且结果仍然很奇怪,这表明你有另一个你没有告诉我们的清单?或者你只是把结果写在记事本......

如果您只想要一个电子邮件列表并计算,这将有效:

select referenceBy, count(1) as referenceEmailCount from (
  select referenceBy from tbl_user1
  union all
  select referenceBy from tbl_user2
) as t
group by referenceBy

如果不是您需要的话,请提供更多信息。

答案 1 :(得分:0)

由于2个表的模式相同,因此您可以执行<div id="myJershy"> <img src="image/Pngs/collar/White.png" id="grey" style="position: fixed; top: 8px; left: 12px; width: 935px;"> <img class="orange" id="orange" src="image/Pngs/body/White.png" style="position: fixed; left: 10px; top: 8px;"> <img src="image/Pngs/sleeves/White.png" id="gold" style="position: fixed; top: 12px; left: 22px;"> <img class="back" id="black" src="image/Pngs/cuffs/White.png" style="position: fixed; left: 11px; top: 9px;"> </div>以获得组合结果,并可以执行外部查询以获得总计数。

union