2021年1月6日总结:聚合查询、分页查询、查询排序、分组查询
杂点整理:
聚合函数不能和普通字段一起查,查的话也只会显示第一个记录,mysql新版本已禁止这样无意义的操作:
| id | name | gender |
| 1 | tom | male |
| 2 | jack | male |
| 3 | alice | female |
| 4 | carrie | female |
如上表:
select gender,count(1) from table1 group by gender;
| gender | count(1) |
| male | 2 |
| female | 2 |
可以显示男性和女性的人数,但是:
select id,count(1) from table1 group by gender;
| id | gender | count(1) |
| 1 | male | 2 |
| 3 | female | 2 |
id显示的是第一个男性的值,没有意义
==============
count 统计不包含null
count(*)是把每个字段的行数都统计一下,然后取最大值
count(1)可以理解成加了一个常量列,并计算行数,在数据很复杂时,count(1)效率更高,因为他不用遍历所有列。
| id | name | gender | age | count(1) |
| 1 | tom | male | (null) | 1 |
| 2 | (null) | female | 12 | 1 |
| (null) | alice | female | 15 | 1 |
=================
分页公式:

=========================
使用order by时,不加asc desc ,order by默认是asc的



=========================
order by多个排序条件:条件1,条件2
按照条件1排列完,有相同值时,再按第二个排

============================
如果需要同时写,先写order by 再写limit

====================
分组之前条件使用where关键字,分组之后条件使用having关键字


===================
EXTRACT(unit FROM date)可以从日期中返回具体年月日等

====================
#查询Student表中不姓“王”的同学记录。
SELECT * FROM student WHERE sname NOT LIKE '王%';
=======================
decimal数据类型:

由此可见decimal小数点后会补零,但是小数点前不会。

浙公网安备 33010602011771号