MySQL 数据查询
程序员文章站
2022-06-25 08:21:16
SELECT子句:用来指定查询返回字段,星号(*)表示返回所有字段 SELECT [DISTINCT]*|字段列表 #DISTINCT 用来过滤重复数据 FROM子句:用来指定数据来源的表 FROM <表名> WHERE子句:用来定义查询返回数据的条件 WHERE 查询条件 GROUP BY子句:用 ......
- select子句:用来指定查询返回字段,星号(*)表示返回所有字段
select [distinct]*|字段列表 #distinct 用来过滤重复数据
- from子句:用来指定数据来源的表
from <表名>
- where子句:用来定义查询返回数据的条件
where 查询条件
- group by子句:用来指定查询结果的分组条件
group by 分组字段 [having 分组条件]
- order by子句:用来给指定字段排序,asc(升序)可省略,desc(降序)
order by 排序字段 [asc|desc] #asc 为升序|desc为降序
- limit子句:用来限制select语句返回的记录数
limit [初始位置],记录数
举例说明:
以student、course、score三张表示范, 三张表的创建语句如下
-- -- database: stuinfo -- create database stuinfo default character set utf8; use stuinfo; -- -- 表的结构 student /*学生表*/ -- create table student ( sno char(10) not null comment '学号', sname varchar(20) not null comment '姓名', sex char(2) not null default '男' comment '性别', birthday date not null comment '出生日期', deptname varchar(30) not null comment '所属班级', remark varchar(80) comment '备注', primary key (sno), /*设置sno为主键*/ index (sname) /*设置sname为普通索引*/ ) engine=innodb default charset=utf8; -- -- 转存表中的数据 student -- insert into student (sno, sname, sex, birthday, deptname, remark) values ('1308013101', '陈斌', '男', '1993-03-20', '软件131', ''), ('1308013102', '张洁', '女', '1996-02-08', '软件131', ''), ('1308013103', '郑先超', '男', '1994-04-25', '软件131', ''), ('1308013104', '徐孝兵', '男', '1994-08-06', '软件131', ''), ('1308013105', '王群', '女', '1995-03-27', '软件131', ''), ('1309122501', '刘威', '男', '1994-07-13', '网络131', ''), ('1309122502', '沈雁斌', '男', '1994-05-28', '网络131', ''), ('1309122503', '杨群', '女', '1995-10-18', '网络131', ''), ('1309122504', '蒋维维', '男', '1994-10-19', '网络131', ''), ('1309122505', '杨璐', '女', '1995-09-26', '网络131', ''), ('1312054901', '王林林', '男', '1994-04-16', '机电131', ''), ('1312054902', '杨一超', '男', '1994-08-27', '机电131', ''), ('1312054903', '张伟', '男', '1995-01-03', '机电131', ''), ('1312054904', '田翠萍', '女', '1994-10-20', '机电131', ''), ('1312054905', '周伟', '男', '1995-09-10', '机电131', ''); -- -- 表的结构 course /*课程表*/ -- create table course ( cno char(5) not null comment '课程编号', cname varchar(30) not null comment '课程名称', credit tinyint unsigned comment '学分', remark varchar(100) comment '备注', primary key (cno), /*设置cno为主键*/ unique (cname) /*设置cname为唯一索引*/ ) engine=innodb default charset=utf8; -- -- 转存表中的数据 course -- insert into course (cno, cname, credit, remark) values ('01001', 'c语言程序设计', 5, '计算机类专业课程'), ('01002', '数据结构', 4, '计算机类专业课程'), ('01003', 'java程序设计', 4, '计算机类专业课程'), ('02001', '网络基础', 3, '计算机类专业课程'), ('02002', '数据库原理及应用', 4, '计算机类专业课程'), ('02003', '操作系统', 4, '计算机类专业课程'), ('09001', '机械设计基础', 5, null), ('09002', '机械制造基础', 4, null), ('09003', '机械制图', 4, null); -- -- 表的结构 score /*成绩表*/ -- create table score ( sno char(10) not null comment '学号', cno char(5) not null comment '课程编号', grade tinyint unsigned not null comment '成绩', primary key(sno, cno), /*设置sno和cno为复合主键*/ foreign key(sno) references student(sno) /*与学生表关联*/ on update no action on delete no action, foreign key(cno) references course(cno) /*与课程表关联*/ on update no action on delete no action ) engine=innodb default charset=utf8; -- -- 转存表中的数据 score -- insert into score (sno, cno, grade) values ('1308013101', '01001', 72), ('1308013101', '01002', 56), ('1308013101', '01003', 77), ('1308013102', '01001', 85), ('1308013102', '01002', 73), ('1308013102', '01003', 90), ('1308013103', '01001', 79), ('1308013104', '01001', 82), ('1308013105', '01001', 63), ('1309122501', '02001', 84), ('1309122501', '02002', 92), ('1309122501', '02003', 71), ('1312054901', '09001', 87), ('1312054901', '09002', 90), ('1312054901', '09003', 95);
查询语句如下:
-- 查询学生表的所有字段 select * from student; -- 查询学生表的学号,姓名,性别字段 select sno,sname,sex from student; -- 查询查询学生表的学号,姓名,性别字段,并用 as 定义字段别名 select sno as '学号',sname as '姓名',sex as '性别' from student; -- 查询学生表的所有女生信息 select *from student where sex='女'; -- 查询课程表中学分大于4的课程所有信息 select *from course where credit>4; -- 查询学生表中在1995年出生的学生信息 select *from student where birthday>='1995-1-1' and birthday<='1995-12-31'; -- 查询学生表中姓杨的信息 select *from student where sname like '杨%'; -- 查询学生表中姓杨的姓名为两个字的学生信息 select *from student where sname like '杨_'; -- 查询学生表中姓杨的姓名为三个字的学生信息 select *from student where sname like '杨__'; -- 查询学生表中姓名的第二个字为伟和先的同学信息 select *from student where sname like '_伟%' or sname like '_先%'; -- 查询学生表中在1995年出生的学生信息 select *from student where birthday between '1995-1-1' and '1995-12-31'; -- 查询成绩表中成绩在60到89之间的信息 select *from score where grade not between 60 and 89; -- 查询学生表学号为 1308013101,1309122503,1312054904 的学生信息 select *from student where sno in('1308013101','1309122503','1312054904'); -- 查询课程表中标记为空的信息 select *from course where remark is null; -- 查询学生表中男生的信息并按生日降序排序 select *from student where sex='男' order by birthday desc; -- 查询学生表所有信息并按性别降序,学号升序排列 select *from student order by sex desc,sno; -- 查询学生表信息并按生日降序,取前五个记录 select *from student order by birthday desc limit 5; -- 查询成绩表中当课程号为01001的学号,课程号,成绩信息,并以成绩降序排列 select sno,cno,grade from score where cno='01001' order by grade desc limit 1,3; -- 查询成绩表中选修的学号 并用distinct去重 select distinct sno from score; -- 用内连接 inner join 来查询信息 select student.sno,sname,sex,cno,grade from student inner join score on student.sno=score.sno where sex='女'; select student.sno,sname,sex,cno,grade from student,score where student.sno=score.sno and sex='女'; -- 查询学号为1308013101 的信息 select s.sno,sname,sex,c.cno,cname,grade from student s inner join score g on s.sno=g.sno inner join course c on c.cno=g.cno where s.sno='1308013101'; select s.sno,sname,sex,c.cno,cname,grade from student s ,course c,score g where s.sno=g.sno and g.cno=c.cno and s.sno='1308013101'; -- 查询学号为1308013101的最高分,最低分及平均分和总分信息 select max(grade) as '最高分',min(grade) as '最低分' ,avg(grade) as '平均分',sum(grade) as '总分' from score where sno='1308013101' ; -- 查询学生表男生人数 select count(*) as '男生人数' from student where sex='男'; -- 查询成绩表中已选修课程的人数并用distinct去重 select count(distinct sno)as '已选修课程学生人数' from score; -- 分组统计男、女生人数 select sex as '性别',count(*) as '学生人数' from student group by sex; -- 分组统计不同班级人数 select deptname as '班级',count(*) as '学生人数' from student group by deptname; -- 5分组查询学生的学号、姓名、选修课程数及平均分 select student.sno as '学号',sname as '姓名', count(*)as '选修课程数',avg(grade) as '平均分' from score inner join student on score.sno=student.sno group by student.sno; -- 用嵌套查询学号为1308013101的同学在同一个班级的学生信息 select *from student where deptname=(select deptname from student where sno='1308013101'); -- 查询课程编号为01001的课程,且成绩超过该课程平均分的学生的学号、姓名、所属班级、课程编号、课程名称及成绩 select student.sno,sname,deptname,course.cno,cname,grade from student,score,course where student.sno=score.sno and score.cno=course.cno and course.cno='01001'and grade>(select avg(grade)from score,course where course.cno=score.cno and course.cno='01001'); -- 创建新数据表,查询网络131的学生记录,将查询结果插入到新数据表中 create table tempstudent( stuno char(10) not null primary key, stuname varchar(20)not null, sex char(1), deptname varchar(30) not null ); insert into tempstudent(stuno,stuname,sex,deptname) select sno,sname,sex,deptname from student where deptname='网络131'; -- 将数据结构 课程成绩统一减去5 update score set grade=grade-5 where cno=(select cno from course where cname='数据结构'); -- 查询更新完的成绩表 select *from score where cno=(select cno from course where cname='数据结构'); -- 删除机电131的学生的成绩记录 delete from score where sno in (select sno from student where deptname='机电131'); -- 查询 删除后的机电131成绩记录 select *from score where sno in (select sno from student where deptname='机电131');