在MySQL中结合2个计数

时间:2015-05-14 14:29:15

标签: mysql select join count

基本上我有2个选项显示计数:

select count(b.i_connection_type) as Incoming_calls 
from (select i_connection 
      from Active_Calls 
      where i_connection is not NULL ) as a 
join Connections as b on a.i_connection=b.i_connection
where b.i_connection_type=2;


select count(*) as "On-Net Calls" from Active_Calls where i_connection = 1;

如何将它们粘在一起,所以它将是一个表:

|On-Net Calls| Incoming Calls|
-------------------------------
|      2     |  4            |

1 个答案:

答案 0 :(得分:1)

您的查询每个返回一个整数,可以投影在一行上,如:

select (select 1) as a, (select 2) as b;
+---+---+
| a | b |
+---+---+
| 1 | 2 |
+---+---+

因此,只需将两个查询嵌套在一个新查询中:

select (
    select count(b.i_connection_type) from (
         select i_connection from Active_Calls 
         where i_connection is not NULL ) as a 
         join Connections as b on a.i_connection=b.i_connection 
         where b.i_connection_type=2
) as "Incoming Calls", (
     select count(*) from Active_Calls where i_connection = 1
) as "On-Net Calls";