Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.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使用group进行嵌套选择_Mysql_Sql - Fatal编程技术网

如何通过mysql使用group进行嵌套选择

如何通过mysql使用group进行嵌套选择,mysql,sql,Mysql,Sql,请给我这张桌子: id| statut | datecreation 1 | appel |10-09-2018 2 | message|10-09-2018 3 | message|11-09-2018 4 | message|11-09-2018 5 | appel |12-09-2018 我想要这样 date |Nbappel |Nbmessage 10-09-2018 | 1 |1 11-09-2018 | 0 |2 12-09-2018 |

请给我这张桌子:

id| statut | datecreation
1 | appel  |10-09-2018
2 | message|10-09-2018
3 | message|11-09-2018
4 | message|11-09-2018
5 | appel  |12-09-2018
我想要这样

date        |Nbappel |Nbmessage
10-09-2018  |   1    |1
11-09-2018  |   0    |2
12-09-2018  |   1    |0
你可以尝试:

SELECT datecreation as date, 
       SUM(case when statut='appel' then 1 else 0 end) as Nbappel,
       SUM(case when statut='message' then 1 else 0 end) as Nbmessage  
  FROM t
 GROUP BY datecreation
 ORDER BY datecreation;

您可以执行条件聚合:

select datecreation, 
       sum( statut = 'appel' ) as Nbappel, 
       sum( statut = 'message' ) as Nbmessage
from table t
group by datecreation;

案例
上使用
总和
,只需搜索条件聚合。非常感谢much@hammani谢谢你,朋友。