Mysql笔记。
–查找学生id为1,3,5的学生
select * from my_student where id=1 || id=3 || id=5;
select * from my_student where id in(1,3,5);
–查找身高在180到190之间的学生
select * from my_student where height >= 180 and height <=190 ;
select * from my_student where height between 180 and 190 ;
Group by子句
Group by分组的意思,根据某个字段进行分组(相同的放一组,不同的分到不同的组)
分组的意义:是为了统计数据(按组统计 :按分组字段进行数据统计)
基本语法 : group by 字段名
count() 统计分组后的记录数 :每一组有多少条记录
max() 统计每组中最大值
min() 统计最小值
avg() 统计平均值
sum() 统计和
–根据性别(sex)分组统计(最高和最矮)身高 ,平均年龄和总年龄
select sex , count(*) ,max(height) , min(height) , avg(age) , sum(age) from my_student group by sex ;
分组会自动排序 :根据分组字段:默认升序
group by 字段 [asc | desc] ;
多字段分组:先根据一个字段进行分组,然后对分组后的结果再次按其他字段进行分组。
–多字段分组:先班级,后男女
select c_id , sex , count(*) from my_student group by c_id , sex ;
以上的结果是查询每个班级的男女数量并统计。
有一个函数:可以对分组的结果中的某个字段值进行字符串连接(保留改组所有的某个字段)
group_concat(字段)
select c_id , sex ,count(*) , group_concat(name) from my_student group by c_id , sex ;