– 1.查询所有学生信息
select * from student
– 2.查询所有学生信息,显示学生姓名,性别,电话
select name,sex,phone from student;
– 3.查询学生在那些班级中,显示学生姓名,性别,电话
– distinct 对查询结果去重
select distinct cid,name,sex,phone from student;
– 4.把所有学生成绩加2分,显示学号,显示加分前的成绩和加分后的成绩
select stuid as 学号,socre 加分前,socre+2 加分后 from score;
– select 第一种子句 where:对查询结果进一步筛选
– 1.查询姓名是李四的学生信息
select * from student where name='李四';
– 2.查询任何一门成绩及格的学生信息,显示学号,科目编号,成绩
select stuid,subid,socre from score where socre>=60
– 3.查询考试成绩在60分到80分之间的学生学号,成绩
– BETWEEN…and :两个值之间,包含两个值,同等有>=和<=
select stuid ,socre from score where socre>=60 and socre<=80
select stuid,socre from score where socre between 60 and 80
– 3.查询考试成绩不在60分到80分之间的学生学号,成绩
select stuid,socre from score where socre not between 60 and 80
– in :在什么什么里面,代表一个集合,只要查询结果满足in里面其中一个元素,就为true,就查询出来
select stuid,socre from score where socre in(85,48,56)
– like :像 喜欢,模糊查询
– %:代表多个字符
select * from student where name like '张%'
– 查询以四结尾的学生信息
select *from student where name like '%四'
– 查询姓名中包含m的学生信息
select * from student where name like '%m%'
– _:代表的是一个字符
select * from student where name like '李_'
select * from student where name like '李__'
– is null :列的值为空查询出来 not相反
select * from score where socre is null;
– 分组函数又叫聚合函数,是Mysql给我们提供的一些方法
– avg(列名):求平均值
– 求成绩表中科目编号是1的平均成绩
select avg(socre) as 科目1的平均值 from score where subid=1;
– max (列名):求最大值,min(列名):求最小值
– 查询参见科目1考试的最高分和最低分
select max(socre) 最高分,min(socre) 最低分 from score where subid=1
– count(列名) :统计表中共有多少记录
– 查询参见科目一考试的学生人数
select count(*) from score where subid=1;
– sum(列名) :求和
– 查询学号为1的学生的总成绩,显示学号,总成绩
select sum(socre),stuid from score where stuid=1;
– 各科目都已经考完,成绩已出来,怎么获取各科目的平均成绩呢?
– 可以实现
– 1.查询有哪些科目参见了考试
select distinct subid from score;
– 2.求每个科目的平均值
– group by :分组,作用于where之后
– 什么时候用分组呢?只要这个题目带‘每’和‘各’的肯定是分组
– 需要使用分组函数的时候,考虑一下是否使用分组
select subid,avg(socre) from score group by subid
– 如果Sqlserver,select后面有几列,group by后面就得加几列,,分组函数除外
– group by 不能单独使用,必须配合having用
select subid,avg(socre) from score group by subid
– 查询出所有成绩加五分后,成绩还是小于60分的学生学号
select socre+5 as 成绩加五分 from score where 成绩加五分<60;
– where :查询结果进行筛选,针对表中真正存在的列起作用
– having :查询结果进行筛选,针对查询结果的列起作用,成绩加五分这一列就是一个查询结果
select stuid, socre+5 as 成绩加五分 from score having 成绩加五分<60;
– SQLserver :having不能单独用,必须配合group by用
– 获取平均成绩大于等于70的各科目的平均成绩
select avg(socre) from score group by subid having avg(socre)>=70
– order by :排序
– 员工大会,排序
– 牌子:排名不分前后,同级员工
– 根据员工的首字母进行排序
– asc:升序,默认为升序
– 查询学生信息并按照班级编号升序
select * from student order by cid asc
– desc:降序
select * from student order by cid desc
– 根据多个字段进行排序,现根据字段1进行排序,然后在排序后的基础上再根据字段2进行排序。。。
select *from student order by cid,birthday ASC
– 查询年龄最大的五名学生信息
– limit (数据的坐标,显示的条数):数据的坐标从0开始
select * from student order by birthday asc limit 0,5
– 交叉连接:就是多个表两两组合后的结果
select * from student cross join classinfo
select *from student join classinfo
select * from student,classinfo
– 查询张三所在班级,显示姓名,电话,班级名
– 等值连接
-- 1.把两个表连接成大表,既然连接成了一个大表,就把这张大表当成单表使用
select name,phone,cname from student,classinfo
-- 2.确定他们之间的连接条件
where student.cid=classinfo.cid
-- 3确定其他筛选条件
and name='张三三'
-- 4确定显示哪些列