来源: 未知  发布时间: 2026-01-20   次浏览
查看当前编码:show variables like 'character_set_%' 
 
insert into ... values ... // 增
delete from ... where ... // 删
update ... set ... where ... // 改
select 查询结果,如: [学号,平均成绩:组函数avg(成绩)]
from 从哪张表中查找数据,如:[涉及到成绩:成绩表score]
where 查询条件,如:[b.课程号='0003' and b.成绩>80]
group by 分组,如:[每个学生的平均:按学号分组](oracle,SQL server中出现在select 子句后的非分组函数,必须出现在group by子句后出现),MySQL中可以不用
having 对分组结果指定条件,如:[大于60分]
order by 对查询结果排序,如:[增序: 成绩  ASC / 降序: 成绩 DESC];
limit 使用limt子句返回topN(对应这个问题返回的成绩前两名),如:[ limit  2 ==>从0索引开始读取2个]limit==>从0索引开始 [0,N-1]
组函数: 去重 distinct()  统计总数sum()   计算个数count()  平均数avg()  最大值max() 最小数min()
 
多表连接: 内连接(省略默认inner) join ...on..
左连接left join tableName as b on a.key ==b.key
右连接right join 
连接union(无重复(过滤去重))和union all(有重复[不过滤去重])
select * from test.student union all select * from test2.student // test2是同服务器里的另一数据库,行合并,且最大列数是与union前面的表列数
 
数据库对象:表(table)  视图(view)  序列(sequence)  索引(index)  同义词(synonym)
视图(view)
create view num as select 学号,姓名 from student
select * from num
show full tables in test where table_type like 'view' // 查询数据库中所有视图
drop view if exists num // 加if exists当视图不存在时不报错,同时删除多个视图只需要视图后面加逗号增加即可
 
update num set 姓名 = '黄子轩' where 学号 = 0007
select * from num where 学号 = 0007
 
索引(index)
1.主键自动建立唯一索引
2.频繁作为查询条件的字段应该创建索引
3.查询中与其他表关联的字段,外键关系建立索引
4.频繁更新的字段不适合创建索引
5.where条件里用不到的字段不创建索引
6.查询中排序的字段,排序字段若通过索引去访问将大大提高排序速度
 
like查找
select * from student where 姓名 like '孟%'
select * from student where 姓名 like '%孟'
select * from student where 姓名 like '%孟%'
 
group by分组
group by 搭配max,min,avg,count,sum等聚合函数使用
 
select 课程号,max(成绩) as 最高分,min(成绩) as 最低分 from score group by 课程号
select 课程号, count(学号) from score group by 课程号
select 性别, count(*) from student group by 性别
 
查询平均成绩大于60分学生的学号和平均成绩
select 学号, avg(成绩) from score group by 学号 having avg(成绩) > 60
查询至少选修两门课程的学生学号
select 学号, count(课程号) as 选修课程数目 from score group by 学号 having count(课程号) >=2
统计性别人数
select 性别,count(*) as 性别人数 from student group by 性别
查询不及格的课程并按课程号从大到小排列
select 课程号, 成绩 from score where 成绩< 60 order by 课程号 desc
查询每门课程的平均成绩,结果按平均成绩升序排序,平均成绩相同时,按课程号降序排列
select 课程号,avg(成绩) as 平均成绩 from score group by 课程号 order by 平均成绩 asc, 课程号 desc 
检索课程编号为“0004”且分数小于60的学生学号,结果按按分数降序排列
select * from score where 课程号 ='0003' and 成绩 < 60 order by 成绩 desc
要求输出课程号和选修人数,查询结果按人数降序排序,若人数相同,按课程号升序排序
select 课程号, count(学号) as 选修人数 from score group by 课程号 having count(学号)> 2 order by count(学号) desc,课程号 asc
查询两门以上课程分数小于90的同学的学号及其平均成绩
select 学号, avg(成绩) as 平均成绩 from score where 成绩 < 90 group by 学号 having count(课程号) >=2
 
子查询
查询所有课程成绩小于90分学生的学号、姓名
select 学号,姓名 from student where 学号 in( select 学号 from score where 成绩 < 90)
查询没有学全所有课的学生的学号、姓名
select 学号,姓名 from student where 学号 in(select 学号 from score group by 学号 having count(课程号) < (select count(课程号) from course))
查询出只选修了两门课程的全部学生的学号和姓名
select 学号,姓名 from student where 学号 in(select 学号 from score group by 学号 having count(课程号) =2)
 
日期函数
current_date、current_time、current_timestamp、year、month、day、dayname
查询1995年出生的学生名单
select 学号,姓名 from student where year(出生日期) = 1995
 
每门课程最高成绩
select 课程号,max(成绩) as 最大成绩 from score group by 课程号
 
以下两句中的区别就是第一句会出现课程号与成绩相同的不同学号
select * from score as a where 成绩 = (select max(成绩) from score as b where b.课程号 = a.课程号)
select 学号,课程号, max(成绩) from score group by 课程号
查询0001科成绩前两名的记录
select * from score where 课程号 = 0001 order by 成绩 desc limit 2
查询0001和0002科成绩前两名的记录
(select * from score where 课程号 = 0001 order by 成绩 desc limit 2) union (select * from score where 课程号 = 0002 order by 成绩 desc limit 2)
 
多表查询
查询所有学生的学号、姓名、选课数、总成绩
select a.学号,a.姓名,count(b.课程号) as 选课数, sum(b.成绩) as 总成绩 from student as a left join score as b on a.学号 = b.学号 group by a.学号
查询平均成绩大于85的所有学生的学号、姓名和平均成绩
select a.学号,a.姓名,avg(b.成绩) as 平均成绩 from student as a left join score as b on a.学号 = b.学号 group by a.学号 having avg(b.成绩) > 85
查询学生的选课情况:学号,姓名,课程号,课程名称
select a.学号, a.姓名, c.课程号,c.课程名称 from student a inner join score b on a.学号=b.学号 inner join course c on b.课程号=c.课程号 order by a.学号
查询出每门课程的及格人数和不及格人数
select 课程号,sum(case when 成绩 >= 60 then 1 else 0 end) as 及格人数,sum(case when 成绩 < 60 then 1 else 0 end) as 不及格人数 from score group by 课程号
使用分段[100-85],[85-70],[70-60],[<60]来统计各科成绩,分别统计:各分数段人数,课程号和课程名称
select a.课程号,b.课程名称,sum(case when 成绩 between 85 and 100 then 1 else 0 end) as '[100-85]',sum(case when 成绩 >= 70 and 成绩<85 then 1 else 0 end) as '[85-70]' from score as a right join course as b on a.课程号 = b.课程号 group by a.课程号,b.课程名称
查询课程编号为0003且课程成绩在80分以上的学生的学号和姓名
select a.学号,a.姓名 from student as a inner join score as b on a.学号=b.学号 where b.课程号=0003 and b.成绩>80
 
表结构转换
select 学号,'课程号0001','课程号0002','课程号0003' from score
select 学号,(case 课程号 when 0001 then 成绩 else 0 end) as '课程号0001',(case 课程号 when 0002 then 成绩 else 0 end) as '课程号0002',(case 课程号 when 0003 then 成绩 else 0 end) as '课程号0003' from score
select 学号,max(case 课程号 when 0001 then 成绩 else 0 end) as '课程号0001',max(case 课程号 when 0002 then 成绩 else 0 end) as '课程号0002',max(case 课程号 when 0003 then 成绩 else 0 end) as '课程号0003' from score group by 学号