在 MySQL 中合并 2 个计数

Combine 2 counts in MySQL

基本上我有 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;

如何将它们粘在一起,所以它会是一个 table 像:

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

您的查询 return 每个一个整数,可以投影在一行中,例如:

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";