mysql 一条语句多种count如何写

查看:83
本文介绍了mysql 一条语句多种count如何写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

现在的统计是这样写的

select column1 title,count(1) count1
FROM table1
WHERE column2 is not null
AND column2 > '2016-06-13'
AND column2 < '2016-06-28'
AND column3 is not null
group BY title
having count1 > 3000

现在要再加一列统计,数据是在这个基础上再加一个条件过滤,
大概就是要下面这样的效果,要怎么写?

select column1 title,count(1) count1,count(column3='abc') count2
FROM table1
WHERE column2 is not null
AND column2 > '2016-06-13'
AND column2 < '2016-06-28'
AND column3 is not null
group BY title
having count1 > 3000

解决方案

select column1 title,count(1) count1, sum(IF(column3='abc', 1, 0)) count2
FROM table1
WHERE 
column2 > '2016-06-13'
AND column2 < '2016-06-28'
AND column3 is not null
group BY title
having count1 > 3000

试试看
条件中的column2 is not null没用,可以去掉。

这篇关于mysql 一条语句多种count如何写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆