当前位置:网站首页>MySQL - count(字段)、count(主键)、count(1)、count(*)的区别
MySQL - count(字段)、count(主键)、count(1)、count(*)的区别
2022-07-29 02:27:00 【不渴望力量的哈士奇】
count(字段)、count(主键)、count(1)、count(*)的区别
- count()是一个聚合函数,对于返回的结果集一行行地判断,如果 count 函数中参数对应的字段值不是 Null,累计值就加 1,否则不加,最后返回累计值。
- count(*)、count(1)和count(主键 id) 都返回满足条件的结果集的总行数;而 count(字段),则返回字段中值不为 Null 的总行数。
以weibo_user表为例,表中共有99条数据,created_at字段值不为Null的数据有6条
- count(*)查询到全部数据
- count(id)查询到全部数据
- count(1)查询到全部数据
- count(create_at)只查询到6条数据,值不为NULL的数据
执行效率
- 如果数据表没有主键,那么count(1)比count()快;
- 如果有主键,那主键作为count的条件也比count()要快;
- 如果表只有一个字段,count(*)就是最快的。
边栏推荐
猜你喜欢

冰冰学习笔记:运算符重载---日期类的实现

Polygon zkEVM——Hermez 2.0简介

区区区间---线段树lazy标记板子题

Wechat applet - Advanced chapter Lin UI component library source code analysis button component (II)

漫画算法_小灰灰面试

This blogger has a comprehensive classification of QT. If you are free, go to study and summarize it and record it.

优炫软件任命黄志军为公司总经理

Rocbos open source micro community light forum source code

Ffmpeg+sdl+qt is a simple video player

新版海螺影视主题模板M3.1全解密版本多功能苹果CMSv10后台自适应主题开源全解密版
随机推荐
HTB-Blocky
10. Writing rules - Document Search
《微信小程序-进阶篇》Lin-ui组件库源码分析-Button组件(二)
Comic algorithm_ Xiaohuihui interview
FTP协议详解
Rocbos open source micro community light forum source code
Wechat applet - Advanced chapter Lin UI component library source code analysis button component (II)
Where, having, group by, order by, is null, not in, subquery, delete, date function
第六天笔记
Memories of many years ago
New conch movie theme template m3.1 fully decrypted version multifunctional apple cmsv10 background adaptive theme open source fully decrypted version
Small program source code for campus stray cat information recording and sharing / wechat cloud development medium big cat spectrum small program source code
STP协议(生成树协议)
C语言:小乐乐与进制转换
Youxuan software appoints Huang Zhijun as the general manager of the company
Cuda-npp image and video processing
owt-server源码剖析(四)--video模块分析之Mixer Out
Family relationship calculator wechat applet source code
C language to achieve the three chess game
owt-server源码剖析(三)--video模块分析之Mixer In




