Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 搜索查询计数两个字段,并将结果分组为一行_Mysql_Select - Fatal编程技术网

Mysql 搜索查询计数两个字段,并将结果分组为一行

Mysql 搜索查询计数两个字段,并将结果分组为一行,mysql,select,Mysql,Select,我有一个表,在其中我想通过orig调用和term调用将其生成一行组。我会数一数它有多少次传入和传出..这是表格 呼叫表 |ID | originating call | terminating call | call type | 1 | 123 | 123 | incoming | 2 | 123 | 123 | outgoing | 3 | 123

我有一个表,在其中我想通过orig调用和term调用将其生成一行组。我会数一数它有多少次传入和传出..这是表格

呼叫表

|ID | originating call |   terminating call |  call type    
| 1 | 123              |       123          |  incoming
| 2 | 123              |       123          |  outgoing
| 3 | 123              |       321          |  incoming
| 4 | 123              |       321          |  incoming
结果应该是

 originating call |   terminating call |  incoming  | outgoing
      123         |       123          |     1      |    1
      123         |       321          |     2      |    0

我尝试将count()作为传入,将count()作为传出,但我不知道下一步该怎么做。我应该按发起呼叫和终止呼叫对其进行分组吗?

这称为条件聚合。您可以使用
case
语句执行基本查询:

select originating, terminating,
       sum(case when calltype = 'incoming' then 1 else 0 end) as incoming,
       sum(case when calltype = 'outgoing' then 1 else 0 end) as outgoing,
from calltable ct
group by originating, terminating;
这在所有数据库中都有效。获取
id
列的步骤因数据库而异。在SQL Server中,您将执行以下操作:

select row_number() over (order by (select NULL)) as id,
       originating, terminating,
       sum(case when calltype = 'incoming' then 1 else 0 end) as incoming,
       sum(case when calltype = 'outgoing' then 1 else 0 end) as outgoing,
from calltable ct
group by originating, terminating;
在MySQL中,您将获得
id
作为:

select @id := @id + 1 as id,
       originating, terminating,
       sum(calltype = 'incoming') as incoming,
       sum(calltype = 'outgoing') as outgoing,
from calltable ct cross join
     (select @id := 0) const
group by originating, terminating;

虽然
case
在MySQL中运行良好,但此版本使用了一种在这种情况下非常方便的简写方式。

这称为条件聚合。您可以使用
case
语句执行基本查询:

select originating, terminating,
       sum(case when calltype = 'incoming' then 1 else 0 end) as incoming,
       sum(case when calltype = 'outgoing' then 1 else 0 end) as outgoing,
from calltable ct
group by originating, terminating;
这适用于所有数据库。获取
id
列的步骤因数据库而异。在SQL Server中,您将执行以下操作:

select row_number() over (order by (select NULL)) as id,
       originating, terminating,
       sum(case when calltype = 'incoming' then 1 else 0 end) as incoming,
       sum(case when calltype = 'outgoing' then 1 else 0 end) as outgoing,
from calltable ct
group by originating, terminating;
在MySQL中,您将获得
id
作为:

select @id := @id + 1 as id,
       originating, terminating,
       sum(calltype = 'incoming') as incoming,
       sum(calltype = 'outgoing') as outgoing,
from calltable ct cross join
     (select @id := 0) const
group by originating, terminating;
虽然
case
在MySQL中运行良好,但此版本使用了一种在这种情况下非常方便的速记方式。

使用以下查询:

select originating, terminating,
       sum(case when calltype ='incoming' then 1 else 0 end) as incoming,
       sum(case when calltype ='outgoing' then 1 else 0 end) as outgoing,
from Table_Name
按起始、终止分组

使用以下查询:

select originating, terminating,
       sum(case when calltype ='incoming' then 1 else 0 end) as incoming,
       sum(case when calltype ='outgoing' then 1 else 0 end) as outgoing,
from Table_Name

按起始、终止分组

这是SQL Server还是MySQL?您真正使用的是哪个数据库?您使用的是什么:
MySQL
sqlserver
?是否有
调用类型
其他显示的两个值?@PM77-1对不起,我使用了mysql。这是SQL Server还是mysql?您真正使用的是哪个数据库?您使用的是什么:
MySQL
sqlserver
?是否有
调用类型
显示的两个值不同?@PM77-1抱歉,我使用了mysql。它不应该是count而不是sum吗?我想我明白了,为什么要使用sum(),因为返回值为1或0?我说得对吗?@Vincent-你明白了。它不应该是count而不是sum吗?我想我明白了为什么你使用sum()因为返回值是1还是0?“我说得对吗?”文森特-你说得对。