Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.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_Sql - Fatal编程技术网

MySQL:筛选表并计算多个列

MySQL:筛选表并计算多个列,mysql,sql,Mysql,Sql,我有一个(SQL)表: 现在,我想为每个用户查询每种类型存在多少条目 例如,如果用户ID为10的用户有10个类型为“customer”的entrys和5个类型为“interrested”的entrys 我如何做到这一点?您似乎需要条件聚合: select userid, sum( type = 'customer' ) as customers, sum( type = 'interessted' ) as interessted from table t whe

我有一个(SQL)表:

现在,我想为每个用户查询每种类型存在多少条目

例如,如果用户ID为10的用户有10个类型为“customer”的entrys和5个类型为“interrested”的entrys

我如何做到这一点?

您似乎需要条件聚合:

select userid, 
       sum( type = 'customer' ) as customers, 
       sum( type = 'interessted' ) as interessted
from table t
where userid = 10
group by userid;
见:
userID    customers    interrested
10        10           5
select userid, 
       sum( type = 'customer' ) as customers, 
       sum( type = 'interessted' ) as interessted
from table t
where userid = 10
group by userid;