MySQL count函数的使用

count()函数在使用时参数好像不能设置为表达式,只能设置成指定字段或*

比如在查询性别为男的成员数目时不能写:

sql 复制代码
select count(gender='male') from user_profile ;

否则直接得到6,也就是等价于select count(gender) from user_profile ;

要查询指定字段带限制/条件时用where条件查询或者group by 分组查询:

sql 复制代码
select count(*) from user_profile where gender='male';
sql 复制代码
#分别得到男生和女生的人数
select count(*) as '人数',gender from user_profile group by gender;
相关推荐
我是一颗柠檬5 小时前
【MySQL全面教学】MySQL面试高频考点汇总Day15(2026年)
数据库·后端·mysql·面试
凯瑟琳.奥古斯特6 小时前
高阶子查询题目精炼
开发语言·数据库·python·职场和发展·数据库开发
身如柳絮随风扬6 小时前
数据库读写分离:从原理到实战,构建高并发系统
数据库·mysql
提笔了无痕7 小时前
RAG存储策略中.md格式的切片与存储怎么处理
数据库·ai·rag
陳土7 小时前
DuckDB精读——基于Getting started with DuckDB
数据库·oracle
凯瑟琳.奥古斯特7 小时前
数据库原理选择题精选
数据库·python·职场和发展
曹牧8 小时前
C#:主线程能够捕获到子线程中的异常
开发语言·数据库·c#
朝阳5818 小时前
MongoDB 副本集从零搭建到生产可用
数据库·mongodb