MySQL进阶
MySQL
MySQL 是最流行的关系型数据库管理系统,在 WEB 应用方面 MySQL 是最好的 RDBMS(Relational Database Management System:关系数据库管理系统)应用软件之一。
MySQL查询
查询所有字段
select * from 表名;
例:
select * from students;
查询指定字段
select 列1,列2,... from 表名;
例:
select name from students;
使用 as 给字段起别名
select id as 序号, name as 名字, gender as 性别 from students;
例:
-- 如果是单表查询 可以省略表名
select id, name, gender from students;
-- 表名.字段名
select students.id,students.name,students.gender from students;
-- 可以通过 as 给表起别名
select s.id,s.name,s.gender from students as s;
在select后面列前使用distinct可以消除重复的行
select distinct 列1,... from 表名;
例:
select distinct gender from students;
条件查询
使用where子句对表中的数据筛选,结果为true的行会出现在结果集中
select * from 表名 where 条件;
例:
select * from students where id=1;
where后面支持多种运算符,进行条件的处理
- 比较运算符
- 逻辑运算符
- 模糊查询
- 范围查询
- 空判断
比较运算符
- 等于: =
- 大于: >
- 大于等于: >=
- 小于: <
- 小于等于: <=
- 不等于: != 或 <>
例:查询编号大于3的学生
select * from students where id > 3;
例:查询编号不大于4的学生
select * from students where id <= 4;
例:查询姓名不是“黄蓉”的学生
select * from students where name != '黄蓉';
例:查询没被删除的学生
select * from students where is_delete=0;
逻辑运算符
- and
- or
- not
例:查询编号大于3的女同学
select * from students where id > 3 and gender=0;
例:查询编号小于4或没被删除的学生
select * from students where id < 4 or is_delete=0;
模糊查询
- like
- %表示任意多个任意字符
- _表示一个任意字符
例:查询姓黄的学生
select * from students where name like '黄%';
例:查询姓黄并且“名”是一个字的学生
select * from students where name like '黄_';
例:查询姓黄或叫靖的学生
select * from students where name like '黄%' or name like '%靖';
范围查询
- in表示在一个非连续的范围内
- between … and …表示在一个连续的范围内
例:查询编号是1或3或8的学生
select * from students where id in(1,3,8);
例:查询编号为3至8的学生
select * from students where id between 3 and 8;
例:查询编号是3至8的男生
select * from students where (id between 3 and 8) and gender=1;
空判断
- is null 空
- is not null 非空
例:查询没有填写身高的学生
select * from students where height is null;
判非空is not null
例:查询填写了身高的学生
select * from students where height is not null;
例:查询填写了身高的男生
select * from students where height is not null and gender=1;
优先级
- 优先级由高到低的顺序为:小括号,not,比较运算符,逻辑运算符
- and比or先运算,如果同时出现并希望先算or,需要结合()使用
排序
语法:
select * from 表名 order by 列1 asc|desc [,列2 asc|desc,...]
注意
- 默认按照列值从小到大排列(asc)
- asc从小到大排列,即升序
- desc从大到小排序,即降序
- 将行数据按照列1进行排序,如果某些行列1的值相同时,则按照列2排序,以此类推
例:查询未删除男生信息,按学号降序
select * from students where gender=1 and is_delete=0 order by id desc;
例:查询未删除学生信息,按名称升序
select * from students where is_delete=0 order by name;
例:显示所有的学生信息,先按照年龄从大-->小排序,当年龄相同时 按照身高从高-->矮排序
select * from students order by age desc,height desc;
聚合函数
总数
count(*)表示计算总行数,括号中写星与列名,结果是相同的
例:查询学生总数
select count(*) from students;
最大值
max(列)表示求此列的最大值
例:查询女生的编号最大值
select max(id) from students where gender=2;
最小值
min(列)表示求此列的最小值
例:查询未删除的学生最小编号
select min(id) from students where is_delete=0;
求和
sum(列)表示求此列的和
例:查询男生的总年龄
select sum(age) from students where gender=1;
-- 平均年龄
select sum(age)/count(*) from students where gender=1;
平均值
avg(列)表示求此列的平均值
例:查询未删除女生的编号平均值
select avg(id) from students where is_delete=0 and gender=2;
分组
group by
- group by的含义:将查询结果按照1个或多个字段进行分组,字段值相同的为一组
- group by可用于单个字段分组,也可用于多个字段分组
group by + group_concat()
例:统计学生的性别和每个性别包含的学生名字
select gender,group_concat(name) from students group by gender;
group by + 集合函数
例:分别统计每个性别的人年龄平均值
select gender,avg(age) from students group by gender;
例:分别统计每个性别的人的个数
select gender,count(*) from students group by gender;
group by + having
- having 条件表达式:用来分组查询后指定一些条件来输出查询结果
- having作用和where一样,但having只能用于group by
例:
select gender,count(*) from students group by gender having count(*)>2;
group by + with rollup
with rollup的作用是:在最后新增一行,来记录当前列里所有记录的总和
例:
select gender,count(*) from students group by gender with rollup;
分页
语法:
从start开始,每页获取count条数据
select * from 表名 limit start,count
例:查询前3行男生信息
select * from students where gender=1 limit 0,3;
每页显示m条数据,当前显示第n页
求第n页的数据
select * from students where is_delete=0 limit (n-1)*m,m
MySQL操作
具体操作:
CREATE DATABASE 07test charset=utf8;
use 07test
-- 创建学生表
create table student(
sno varchar(20) primary key,
sname varchar(10) not null,
ssex varchar(10) not null,
sbirthday datetime,
class varchar(20)
);
-- 创建教师表
create table teacher(
tno varchar(20) primary key,
tname varchar(20) not null,
tsex varchar(20) not null,
tbirthday datetime,
pro varchar(20) not null,
depart varchar(20) not null
);
-- 创建课程表
create table course(
cno varchar(20) primary key,
cname varchar(20) not null,
tno varchar(20) not null,
foreign key(tno) references teacher(tno)
);
-- 创建成绩表
create table score(
sno varchar(20) not null,
cno varchar(20) not null,
degree decimal(10, 0),
foreign key(sno) references student(sno),
foreign key(cno) references course(cno),
primary key(sno, cno)
);
-- 1.查询student表中的所有记录
select * from student;
-- 2.检查student表中的所有记录的sname,ssex和class列。
select sname, ssex, class from student;
-- 3.查询老师所有的单位即不重复的depart distinct
select distinct depart from teacher;
-- 4.查询socre表中成绩在从68到80之间的记录。
select * from score where degree between 68 and 80;
-- 5.查询score表成绩中为85、86或88。
select * from score where degree in(85, 86, 88);
-- 6.查询student表中“95031”班或性别为“女”同学的同学记录
select * from student where class='95031' or ssex='女';
-- 7.以class降序查询student表的所有记录
select * from student order by class desc;
-- 8.以cno升序,degree降序查询score表的所有记录
select * from score order by cno asc, degree desc;
-- 9.查询“95031”班学生人数。
select count(*) from student where class='95031';
-- 10.查询score表中的最高分的学生学号和课程号(子查询或者排序)
select max(degree) from score;
select sno,cno from score where degree=(select max(degree) from score);
select sno,cno,degree from score order by degree desc limit 0,4;
-- 11.查看每门课的平均成绩 (分组查询)
select cno, avg(degree) from score group by cno;
-- 12.查询score表中至少有2名同学选修的并以3开头的课程的平均分数 havimg(组级过滤)
-- having 条件表达式:用来分组查询后指定一些条件来输出查询结果
-- having作用和where一样,但having只能用于group by
1.将成绩表按课程分组
select * from score group by cno;
2.将分组后的结果按至少2名同学分
select * from score group by cno having count(cno) >= 2;
3.将课程名以3开头
select * from score where cno like '3%' group by cno having count(cno) >= 2;
select cno,avg(degree), count(*) from score group by cno having count(cno) >= 2 and cno like '3%';
-- 13.查询分数大于70,小于90的sno列(条件查询)
select sno, degree from score where degree between 70 and 90;
select sno, degree from score where degree>70 and degree<90;
-- 14.查询所有学生的 sname,cno,和degree 列。(多表查询)
select sno, sname from student;
select cno, degree from score;
select sname, cno, degree from student, score where student.sno = score.sno;
-- 15.查询所有学生的sno,cname和degree
select cno, cname from course;
select sno, cno, degree from score;
select sno, cname, degree from course, score where course.cno = score.cno;
-- 16.查询所有学生的sname,cname和degree(三表联查)
select sname, cname, degree from student, course, score where student.sno = score.sno and course.cno = score.cno;
-- 17.查阅“95031"班学生每门课的平均分
select * from student where class='95031';
select * from score where sno in (select sno from student where class='95031');
select sno, avg(degree) from score where sno in (select sno from student where class='95031') group by cno;
-- 18.查询选修”3-105“课程的成绩高于”109“号同学”3-105“成绩的所有同学的记录
1.先查询”109“号同学”3-105“成绩
select degree from score where sno='109' and cno='3-105';
2.再找到选修”3-105“课程的成绩高于”109“号同学”3-105“成绩
select * from score where cno='3-105' and degree>(select degree from score where sno='109' and cno='3-105');
-- 19.查询成绩高于学号109,课程号为3-105的成绩的所有记录 子查询
1.先查询”109“号同学”3-105“成绩
select degree from score where sno='109' and cno='3-105';
2.再查询高于他成绩的
select * from score where degree>(select degree from score where sno='109' and cno='3-105');
-- 20.查询和学号为108.101的同学同年出生的所有学生的sno。sname和sbirthday的函数与带in 关键字的子查询
1.先查询和学号为108.101的同学的生日
select year(sbirthday) from student where sno in (108,101);
select sno, sname, sbirthday from student where year(sbirthday) in (select year(sbirthday) from student where sno in (108,101));
-- 21.查询”张旭“教师任课的学生成绩
1.先查询教师的id号
select tno from teacher where tname="张旭";
2.然后根据教师的id号来查询课程id
select cno from course where tno=(select tno from teacher where tname="张旭");
3.查询”张旭“教师任课的学生成绩
select degree from score where cno = (select cno from course where tno=(select tno from teacher where tname="张旭"));