如何使用2个表获得订户数量

时间:2016-04-25 11:16:16

标签: java mysql netbeans-8

我目前正在开发一个java应用程序,它可以让我获得每个移动网络运营商的订户数量。 为此,我创建了一个包含所有运算符的表,我称之为indicatifs,它包含3行:country,operator和mobile_prefix。 我的第二个表名为subs,包含两行:mobile_prefix和订阅者数量 我应该使用什么查询来获得这样的信息:

Operator   Country   Number of Subscribers
SFR        France    20000
T-Mobile   USA       10250
Telia      Sweden    5248

.. 任何形式的帮助将非常感激。 谢谢!

3 个答案:

答案 0 :(得分:0)

select a.operator,a.country,b.number_of_subscriber from table1 a left join table2 b on a.mobile_prefix = b.mobile_prefix;
这是你想要的吗? 请检查

答案 1 :(得分:0)

此查询需要joingroup by。您想使用left join - 假设您想要所有运营商,即使是那些没有订户的运营商:

select i.operator, i.country, count(s.mobile_prefix) as numsubs
from indicatifs i left join
     subs s
     on i.mobile_prefix = s.mobile_prefix
group by i.operator, i.country
order by numsubs desc;

答案 2 :(得分:0)

试试这种方式

select a.country, a.operator, count(*) as subscriber
from indicatifs a
left join subs b
 on a.mobile_prefix = b.mobile_prefix
group by a.operator, a.country