1 ##排序order by
2 ##按照年龄排序 ,默认是升序
3 ##asc:升序 desc:降序
4 select * from student1 order by sage desc;
5 ##把学号不为5的所有学生的学号降序排列。
6 ##先条件后排序
7 select * from student1 where sid<>5 order by sid desc;
8 ##按照学号年龄降序排列的同时学号升序排列。
9 ##先年龄后学号
10 select * from student1 order by sage desc,sid;
11 ##总结:优先级顺序:年龄>学号
12 ##结果:在排序过程中优先级高的(sage)且值重复的情况下,按照次优先级(sid)排序。
13 ##分组:group by
14 ##查询学生表中,学生来自那些地方
15 select distinct saddress from student1 where saddress is not null;
16 select saddress from student1 where saddress is not null group by saddress;
17 ##统计每个地方有多少人
18 select saddress,count(saddress) 人数 from student1 where saddress is not null group by saddress;
19 ##统计男女各有多少人
20 select ssex ,count(ssex) 人数 from student1 group by ssex;
21 ##统计各个地方男女各有多少人
22 select saddress,ssex,count(sid) 人数 from student1 where saddress is not null group by saddress,ssex;
23 ##统计各个地方男女年龄为18岁的各有多少人
24 select saddress,ssex,count(sid) 人数 from student1 where saddress is not null and sage=18 group by saddress,ssex;
25 ##统计各个地方男女各年龄阶段的各有多少人
26 select saddress,ssex,sage,count(sid) 人数 from student1 where saddress is not null group by saddress,ssex,sage;
27 ##统计各个地方男女各年龄阶段的各有多少人,按照人数降序
28 select saddress,ssex,sage,count(sid) 人数 from student1 where saddress is not null group by saddress,ssex,sage order by count(sid) desc;
29 ##统计每个地方人数超过3人的地方
30 select saddress,count(*) 人数 from student1 where saddress is not null group by saddress having count(*)>3;
31 ##总结having是过滤分组后的结果的。
32 ##统计每个地方男女人数超过2人的地方
33 select saddress,ssex,count(*) 人数 from student1 where saddress is not null group by saddress,ssex having count(*)>2;
34 ##统计每个地方男生人数为大于等于2人的地方
35 select saddress,count(*) 人数 from student1 where saddress is not null and ssex='男' group by saddress having count(*)>=2;
1 #查询学生的性别和名字。
2 select ssex,sname from student;
3 #查询18岁以下的女性的名字。
4 select sname from student where sage<18 and ssex='女';
5 #查询16岁以上或者女性的名字和电话。
6 select sname,stel from student where sage>16 or ssex='女';
7 #查询学号不是7号的男性。
8 #方式1:
9 select * from student where sid<>7 and ssex='男';
10 #方式2:
11 select * from student where sid!=7 and ssex='男';
12 #方式3:
13 select * from student where not sid=7 and ssex='男';
14 #方式4:(子查询)in
15 select * from student where sid in(select sid from student where sid!=7);
16
17
18 #排序的 order by desc:降序 asc:升序(可以省略不写)
19 #把年龄升序排列
20 select * from student order by sage asc;
21 #查询学号不是7号的男性的结果降序排列。(3种方式)
22 select * from student where sid!=7 and ssex='男' order by sage desc;
23 #总结:order by要放在where的后面,因为要对查询出的结果进行排序。
24 #把学生表中的年龄升序排列且学号降序。
25 select * from student order by sage asc;
26 select * from student order by sid desc;
27 select * from student order by sage asc,sid desc;
28 #总结:先年龄升序,年龄相同的时学号才会降序排列。
29
30 #模糊查询
31 #查询姓张的。
32 select * from student where sname like '张%';
33 #查询名字中第二为‘梅’。
34 select * from student where sname like '_梅%';
35 #%:代表0个或多个
36 #_:占位符。一个位置。
37 #查询不姓张的。
38 select * from student where sname not like '张%';
39
40 #添加一个地址列,到学生表中
41 alter table student add saddress varchar(30);
42
43 #查询地址为空的。
44 select * from student where saddress is null;
45 #查询地址不为空的。
46 select * from student where saddress is not null;
47
48
49 #子查询(查询中嵌套查询)
50 #执行顺序:先执行()中的,再执行()外面的。
51 #查询和张三同岁的。
52 #1.查询张三的年龄。
53 #2.查询同岁的人
54 select * from student where sage=(select sage from student where sname='张三');
55
56 #查询学号为6 7 8 9 。
57 #方式1:
58 select * from student where sid in(6,7,8,9);
59 #方式2:
60 select * from student where sid=6 or sid=7 or sid=8 or sid=9;
61 #查询学号不为6 7 8 9 。
62 #方式1:
63 select * from student where sid not in(6,7,8,9);
64 #方式2:
65 select * from student where sid!=6 or sid<>7 or sid<>8 or sid<>9;
66
67 起别名
68 分组
69
70
71 #起别名
72 #方式一:
73 select sname as 名字 from student;
74 #方式二:
75 select sname 名字 from student;
76
77 #聚合函数 max min sum avg count
78 #求年龄最大的人
79 select max(sage) from student;
80 #求学号的最小值
81 select min(sid),sname,stel,sage,ssex from student;
82 #求年龄的和
83 select sum(sage) from student;
84 #求年龄的平均值
85 select avg(sage) from student;
86 #有多号人
87 select count(*) from student;
88
89 #求比班级平均年龄大的人的信息
90 select * from student where sage>(select avg(sage) from student);
91
92 ######因为聚合函数不能出现的where条件中。
93
94 #分组group by
95 #按照年龄分组
96 select * from student group by sage;
97 #按照性别分组
98 select * from student group by ssex;
99 #查询再长沙的男生数量和女生数量
100 select count(sid),ssex from student where saddress='长沙' group by ssex;
101 #查询各个地方的男生数量和女生数量
102 select count(*),ssex,saddress from student group by ssex,saddress;
103 #查询各个地方的男生数量和女生数量,并按照数量降序排列
104 select count(*),ssex,saddress from student group by ssex,saddress order by count(*) desc;
105
106 #当where条件和group by和order by 同时出现在一个题目中时的顺序:先where条件 group by order by (具体题目根据具体的题目来)
107
108
109 #having语法结构 一般和group by一起使用。且出现在group by的后面,用来对分组后的结果进行过滤。
110 #查询各个地方的男生数量和女生数量,求大于等于3人的队
111 select count(*),ssex,saddress from student group by ssex,saddress having count(*)>=3;
112
113
114 #查询武汉的男生数量和女生数量,求大于等于3人的队
115 select count(*),ssex,saddress from student where saddress='武汉' group by ssex having count(*)>=3;