做实验的表:
MariaDB [test]> select * from prefix_user;
+---------+-----------+-----+-------+
| user_id | username | age | email |
+---------+-----------+-----+-------+
| 2 | username2 | 2 | NULL |
| 3 | username3 | 111 | 1 |
| 4 | username3 | 10 | 1 |
| 5 | username3 | 10 | NULL |
| 6 | username3 | 10 | NULL |
+---------+-----------+-----+-------+
5 rows in set (0.01 sec)
一般情况:
直接用count() 函数
select count(*) from 表 where 条件;
或者
select count(主键) from 表 where 条件;
其中主键和*的区别主要是: * 会统计 null 的行,
select count(*) from prefix_user where 1; // 5行
select count(email) from prefix_user ;// 2行,忽略了值为null的
当和group by 混用的时候, count() 函数返回的结果是group by之后的结果,例如
MariaDB [test]> sele