如何在MySQL中结合来自同一个表的查询

我有两组结果:

SELECT name,count(appearance) as countA from table where results = '1'
SELECT name,count(appearance) as countB from table where results = '2'

我希望将它们并排组合,如下所示:

+---------+---------+---------+
| col_1   | countA  | countB  |
+---------+---------+---------+
| John    |    3    |    1    |
| Mary    |    1    |    2    |
| Gary    |    2    |   NULL  |
| Sean    |    4    |   NULL  |
| Mike    |  NULL   |    6    |
+---------+---------+---------+

我怎么做?

最佳答案
这应该(在Oracle中)而不需要自联接

SELECT name,sum( case results when '1' then 1 else 0 end ) as countA,sum( case results when '2' then 1 else 0 end ) as countB
  from table 
 where results IN ( '1','2' )
 group by
       name

相关文章

目录MySQL卸载环境查看是否已安装MySQL卸载mysql服务查看是否...
目录数据类型数据类型分类数值类型以TINYINT认识整型族有符号...
目录表的约束空属性非空约束(NOT NULL Constraint)默认值定...
目录函数时间日期函数:字符串函数数学函数其他函数 函数 时间...
目录使用C语言连接库的安装C APImysql_initmysql_real_conne...
目录用户用户管理查询所有用户查看当前用户查看当前连接数创...