加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

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

发布时间:2020-12-11 23:50:33 所属栏目: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 | c

我有两组结果:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读