1、示例
计算每一个同学的总成绩从大到小排名(显示字段有name,总成绩)
不加DESC,默认从小到大排序(ASC)
SELECT NAME,SUM(score) AS t FROM u GROUP BY NAME ORDER BY t DESC
计算每一个同学的最高成绩(显示字段有name,总成绩)
SELECT NAME,MAX(score) AS t FROM u GROUP BY NAME
2、示例
需求,A表关联了B表,如果给A表插入数据
就按照正常的逻辑插入数据,但是如果插入的id在B表中找不到,就会报错
INSERT INTO employee(NAME,dept) VALUE("小明",1); //1是B表的id
3、示例
关联表的问题,如果from表的时候取了别名,后面只能使用 别名.id
SELECT * FROM realauth AS r LEFT JOIN logininfo ON r.`applier_id` = logininfo.`id`
4、示例
一张表关联多张表
SELECT r.id,ap.id AS ap_id,ap.username,au.id AS au_id,au.username FROM realauth AS r LEFT JOIN logininfo AS ap ON r.`applier_id` = ap.`id` LEFT JOIN logininfo AS au ON r.`auditor_id` = au.`id`
5、示例
查出字段不为空和为空的数据
SELECT * FROM userfile WHERE fileType_id IS NOT NULL; SELECT * FROM userfile WHERE fileType_id IS NULL;
6、示例
<select id="getClientSchedules" resultMap="CourseScheduleMap"> SELECT cs.id as scheduleId,ci.courseName,tch.userName teacherName,sv.venueName,sc.campusName, cs.weekTimes,cs.beginTimes,cs.endTimes,cs.rebuildLim,cs.genderLim,cs.totalLim,cs.lastLim, cs.totalCount,cs.maleScale,cs.maleCount,cs.femaleScale,cs.femaleCount,stime.`shortName` beginTimesShortName,etime.`shortName` endTimesShortName FROM (select * from course_schedule where status = 1 and schoolId = #{schoolId} <if test="courseId != null"> and courseId = #{courseId} </if> <if test="teacherId != null"> and teacherId = #{teacherId} </if> <if test="beginTimes != null"> and beginTimes = #{beginTimes} </if> <if test="endTimes != null"> and endTimes = #{endTimes} </if> <if test="weekTimes != null"> and weekTimes = #{weekTimes} </if> <if test="rebuildLim != null and rebuildLim==2"> and (rebuildLim = 0 or rebuildLim = 2) </if> <if test="rebuildLim != null and rebuildLim==1"> and (rebuildLim =0 or rebuildLim = 1) </if> ) cs LEFT JOIN user_info tch ON cs.teacherId= tch.id LEFT JOIN course_info ci ON cs.courseId=ci.id LEFT JOIN school_venue sv ON cs.venueId = sv.id LEFT JOIN school_campus sc ON sv.campusId = sc.id LEFT JOIN school_year sy ON cs.yearId = sy.id LEFT JOIN school_times_info stime ON stime.`times` = cs.`beginTimes` and stime.status=1 and cs.schoolId= stime.schoolId LEFT JOIN school_times_info etime ON etime.`times` = cs.`endTimes` and etime.status=1 and cs.schoolId= etime.schoolId LEFT JOIN course_schedule_depart dep ON cs.id= dep.scheduleId LEFT JOIN course_schedule_major maj ON cs.id= maj.scheduleId LEFT JOIN (select * from user_info where id = #{stuId}) stu ON cs.schoolId= stu.schoolId LEFT JOIN school_class scl ON stu.classId= scl.id and scl.status=1 LEFT JOIN course_class_selection ccs ON ccs.classId = stu.classId where sy.isThisYear=1 and (dep.departId is null or dep.departId=stu.departmentId) and (maj.majorId is null or maj.majorId=stu.majorId) and (ccs.id is null or (cs.weekTimes=ccs.weekTimes and cs.beginTimes>=ccs.beginTimes and ccs.endTimes>=cs.endTimes)) and ((stu.userSex=0 and genderLim!=1) or (stu.userSex=1 and genderLim!=2)) </select>
7、获取数据库中时间的后几天
语法结构
DATE_SUB(date,INTERVAL expr type) DATE_ADD(date,INTERVAL expr type) select * from table where date_sub('2019-04-03',INTERVAL 1 year);
SELECT DATE_ADD(weekBegin,INTERVAL 1 DAY) FROM school_week WHERE WEEK>=1 AND WEEK<=4 AND yearId=22 SELECT DATE_SUB(weekBegin,INTERVAL -1 DAY) FROM school_week WHERE WEEK>=1 AND WEEK<=4 AND yearId=22
比较时间(更多参考:https://www.cnblogs.com/Darkqueen/p/9264087.html)
https://blog.csdn.net/qq_23375733/article/details/88533006
select * from attend_record where status = 1 and schoolId=#{schoolId} and stuId = #{stuId} and unix_timestamp(#{signInTime})> unix_timestamp(signInTime)
8、获取薪水表中第二高的薪水
思路1:从大到小排序,取第二
select distinct salary from Employee order by salary desc limit 1,1
思路2:不是最大数的最大数
SELECT max(Salary) SecondHighestSalary FROM Employee where Salary != (select max(Salary) from Employee );
9、分数排名
题目:letcode算法(https://leetcode-cn.com/problems/rank-scores/)
下面这个应该只能支持mysql8.x
SELECT Score, dense_rank() over(order by Score desc) as 'Rank' FROM Scores
方式2
select a.score as Score, count(DISTINCT b.score) AS Rank # 统计b表符合条件的不重复的分数的数量作为排名 FROM scores a join scores b where b.score >= a.score # 条件是这个分数不小于我,因为a、b表数据相同,所以排名值最小是1 group by a.id # a表中每个数据都进行排名 order by a.score DESC # 最后按分数(跟排名一样)降序排列
10、查询重复数据
1.having只能用在group by之后,对分组后的结果进行筛选(即使用having的前提条件是分组)。
select Email from Person group by Email having count(Email) > 1