mysql>select department from teacher; -- 不去重 mysql>selectdistinct department from teacher; -- 去重
查询区间
1 2 3
mysql>select*from score where degree between60and80; mysql>select*from score where degree >60and degree <80; mysql>select*from score where degree in (85,86,88); -- 特定选项
与或
1 2
mysql>select*from student where class =95031or sex ='女'; mysql>select*from student where class =95031and sex ='女';
mysql>select name,c_no,degree from student,score ->where student.no = score.s_no; -- 查询所有人的班级和成绩
简单嵌套
1 2 3 4
mysql>select c_no,avg(degree) as degree -- 查询特定班级的所有学科的平均成绩 ->from score ->where s_no in (selectnofrom student where class ='95031') ->groupby c_no;
复杂嵌套
1 2 3 4 5 6 7 8
-- 查询3-105课程成绩高于109号同学的所有人的信息 mysql>select*from student ->where student.no in -> (select student.no from student,score where -> score.c_no ='3-105'andno= s_no ->and degree> -> (select degree from score where s_no ='109'and c_no ='3-105') -> );
1 2 3 4 5
-- 查询张旭教授的课程的全部成绩 mysql>select*from score where -> c_no in (selectnofrom course ->where t_no = (selectnofrom teacher ->where name ='张旭'));
1 2 3 4 5
-- 查询每个成绩的排名 select a.Score , (selectcount(distinct b.score) from scores b where a.score<=b.score) Rank from Scores a orderby Score desc;
查找连续出现3次的数字
1 2 3 4 5 6 7 8 9 10
selectdistinct l1.num as ConsecutiveNums from Logs l1,Logs l2,Logs l3 where l1.Id = l2.Id-1and l2.Id = l3.Id-1and l1.Num = l2.Num and l2.Num = l3.Num ;
查找重复出现的元素
1 2 3 4
select Email from Person groupby Email havingcount(Email) >1;
查找每个部门的最高工资的人
1 2 3 4 5 6 7 8 9
select Department.Name Department, Employee.Name Employee, Salary from Employee join Department on Employee.DepartmentId = Department.Id and (DepartmentId, Salary) in (select DepartmentId, max(Salary) from Employee groupby DepartmentId );
交并补
Union求并集
1 2 3 4 5 6 7
mysql>select*from teacher where profession ->notin (select profession from teacher where department = "计算机系") ->and department = "电子工程系" ->union ->select*from teacher where profession ->notin (select prfession from teacher where department = "电子工程系") ->and department = "计算机系";
复制表查找
1 2 3 4
-- 选择成绩低于平均分的成绩信息 mysql>select*from score a where -> degree < (selectavg(degree) from score b where -> a.c_no = b.c_no);
按条件分组查找
1 2 3 4
-- 查找男生多于1个的班级 mysql>select class from student ->where sex ='男' ->groupby class havingcount(*)>1;