mysql count 实现复杂统计解析
mysql的count函数可以计算符合条件的记录条数,比如:
select count(*) from users;
执行结果:
上面的sql只是将查询到的记录总数输出,count函数本身还可以配合if
函数实现更复杂的计数:
select count(if(status = 1, 1, null)) from users
注意,count会将所有非null值计数,所以if里面不符合条件应该返回null。
如果需要按某个字段计算去重后的数量,则需使用 distinct
关键字:
select count(distinct last_name) from users
当需要对1:n
的关联查询做统计时,以上简单的count使用方式就不足以实现需求了,比如有以下两个表:
CREATE TABLE `articles` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT,`title` varchar(255) NOT NULL COMMENT '标题',PRIMARY KEY (`id`)
);CREATE TABLE `posts` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT,`status` tinyint(4) NOT NULL ,`article_id` int(10) unsigned NOT NULLPRIMARY KEY (`id`)
)
要在一条sql中查询出所有有帖子(posts)的文章(articles)的数量has_post_cnt
、无帖子的文章数量not_post_cnt
。
首先必定要做连接来关联文章和帖子:
select * from articles left join posts on posts.article_id = articles.id
一般的思路是连接后再按artiles.id
分组,再在外层对posts.id
判断是否等于null:
select count(if(p_id is not null, 1, null)) has_post_cnt, count(if(p_id is null, 1, null)) not_post_cnt from (select articles.id a_id, posts.id p_id from articles left join posts on posts.article_id = artiles.id group by articles.id
) tmp
但这样的话子查询效率低,考虑不使用子查询的方式实现,首先就必须去掉分组,不然查询结果只能是按分组聚合的结果,出不了所需的计数,首先看这个sql:
select count(if(posts.id is not null, 1, null)) has_post_cnt, count(if(posts.id is null, 1, null)) not_post_cntfrom articles left join posts on posts.article_id = artiles.id
查询出来的not_post_cnt
肯定是对的,因为一条没有帖子的文章和帖子表的连接也就是和null连接,肯定是1:1的,不会有重复记录。
而has_post_cnt
由于没有对artiles.id做分组,所以是1:n的,这个数是文章的帖子记录数,考虑同一个文章的帖子记录的article_id
字段是唯一的,所以使用distinct
来做去重处理:
select count(distinct if(posts.id is not null, posts.article_id, null)) has_post_cnt, count(if(posts.id is null, 1, null)) not_post_cntfrom articles left join posts on posts.article_id = artiles.id group by articles.id
这里的关键是count(distinct if(p_id is not null, posts.article_id, null))
,先做了去重再做了计数,得到的结果即是有帖子记录的文章数。
mysql count 实现复杂统计解析相关推荐
- mysql多条件count_Mysql中使用count加条件统计
Mysql中使用count加条件统计 0 753 坚持奋斗 2019/6/28 13:19:56 江苏 南京 新建一个Mysql数据表a,包含id和num两个字段 [pre] mysql> ...
- MySQL count(*)、count(1) 和count(字段)的区别以及count()查询优化手段
MySQL的count(*).count(1) 和count(字段)的区别以及count()查询优化手段. 文章目录 1 几种count查询的区别 2 优化COUNT()查询 1 几种count查询的 ...
- MySQL count()函数
转载自 MySQL count()函数 MySQL COUNT()函数介绍 COUNT()函数返回表中的行数. COUNT()函数允许您对表中符合特定条件的所有行进行计数. COUNT()函数的语法 ...
- mysql count优化_MySQL count()函数及其优化
很简单,就是为了统计记录数 由SELECT返回 为了理解这个函数,让我们祭出 employee_tbl 表 所有记录 统计行的总数 计算 Zara 的记录数 注意:由于 SQL 查询对大小写不敏感,所 ...
- Mysql count 的多种使用方式性能比较
Mysql的count函数用于统计符合条件的记录数,常用的方式有: 1.count(*) 2.count(1) 3.count(id) 4.count(col) 首先需要明确一点:count函数对于返 ...
- mysql count 1_高性能MySQL count(1)与count(*)的差别
-------------------------------------------------------------------------------------------------第一篇 ...
- MySQL——count()
MySQL--count() count():用于统计数据库表中数据的函数 常见的使用方式:count(*).count(1).count(列名).count(distinct 列名) 函数 统计 统 ...
- Oracle Decode 函数用法 Count函数条件统计 Sign 函数用法 Case When 函数
Oracle Decode 函数用法 Count函数条件统计 Sign 函数用法 Case When 函数 一.概述 1.本文系统的整理了,Oracle中常见的几个函数用法,都是涉及到条件判断分支类的 ...
- MySQL count(1) , count(*), count(列名) 的异同
count 函数主要用于统计行数,我们一般会用 count(1) , count(*), count(列名) 来统计行数,但是这三者之间有什么差距呢? 异同 当 count(列名) 在列名不是主键列的 ...
最新文章
- 卸载Notepad++!事实已证明,它更牛逼……
- string to byte[]
- python 读取word 题库_Python-docx 读取word.docx内容
- Storm 01_初识初知
- USNews世界大学排名! CS引争议!中国高校霸榜:清华第一,北邮反超CMU
- linux一切皆是文件_LINUX一切皆文件
- 网关在链路上什么意思_银行卡上的数字都什么意思
- iOS键盘遮挡输入框,输入区域自动上移
- 拥有自我意识、自我模拟的粒子机器人来了
- DOS 下编写带有日期的脚本例子
- 【windows】win键失效
- Xamarin.Forms 解决ListView高度问题
- optimizer.zero_grad()
- 《前端中文入门手册》.pdf
- 二叉树寻找节点x的所有祖先
- Homestead 安装 phpMyAdmin 作为数据库管理客户端 — Laravel 实战 iBrand API 教程
- 出现Whitelabel Error Page的解决方法(持续更新带图并总结了其他博文)
- 数据结构(C语言)二叉树的链式存储与操作 11月18日
- 手把手带你使用EFR32 -- 土壤湿度传感器变身第二形态,以 ZigBee 形态出击
- 打印机出现黄色感叹号!无法查看属性和设置,开机查看打印机,打印自动变灰色问题无法使用!
热门文章
- linux免费计费系统下载,思谷电话计费系统 Linux版
- Java实现Spark-Redis,Spark 之 Redis on spark java.io.NotSerializableException
- java 切换目录,java-使用SVNKit,如何将目录切换到其他分支?
- http代码_一行代码就可以实现HTTP文件服务器,他为什么写了150行?
- 百度SEO百度站长提交工具 v0.2
- eclipse打包项目为aar_新生日常牢骚之作业打包
- LeetCode206 | Reverse Linked List (Easy)
- java是纯_让你真正了解Java(纯干货)
- java jar 打包库_Java之 将程序打包成jar包
- jQuery.noop()方法