MySQL之select语法
select语句是在所有数据库操作中使用频率最高的SQL语句
单表查询
首先要先有表
简单查询
#专业表
create database selectTest;
use selectTest;
create table specialty(
zno varchar(4) not null primary key,
zname varchar(50) not null
);
#课程表
create table course(
cno varchar(8) not null primary key,
cname varchar(50) not null ,
ccredit int not null,
cdept varchar(20) not null
);
#学生表
create table student(
sno varchar(10) not null primary key,
sname varchar(20) not null,
ssex enum('男','女') not null,
sbrith date not null,
sclass varchar(10) not null,
szno varchar(4) null,
foreign key(szno) references specialey(zno)
);
#选修表
create table sc(
sno varchar(10) not null,
cno varchar(8) not null,
grade float(4) not null,
primary key(sno,cno),
foreign key(sno) references student(sno),
foreign key(cno) references course(cno)
);
1.查询所有学生的信息
select zno, sclass,sno,sname,ssex sbrith from student;
select * form student;
2.指定字段查询,查询学生的学号和姓名
select sno,sname from student;
3.避免重复数据查询:distinct
查询在student表中的班级
select distinct sclass from student;
4.为表和字段取别名
查询学生的学号,成绩,并指定返回结果中的列名为学号,成绩,而不是sno,grage.
select sno'学号',grade'成绩' from sc;
查询sc表中学生的成绩提高10% ,对显示后的成绩列,显示为“修改后的成绩”
select sno,grade,grade*1.1 as '修改后的成绩' from sc;
条件查询:where
1.带关系运算符和逻辑运算符的查询
查询成绩大于90分学生的学号和成绩
select snow,grade from sc where grade>90;
查询成绩在70~80分之间学生的学号和成绩
select sno,grade from sc where grade>=70 and grade<=80;
2.带IN关键字的查询
查询成绩在集合(65,75,85,95)中的学生的成绩和学号
select sno,grade from sc where grade in (65,75,85,95);
3.带between and 关键字的查询
查询成绩在70~80分之间学生的学号和成绩
select sno,grade from sc where grade between 70 and 80;
4.带IS NULL 关键字的空值查询
查询还没有分专业的学生的学号和姓名
select sno,sname,zno from student where zno is null;
5.带LIKE关键字的查询
使用like关键字来匹配一个完整的字符串 ‘蓝莓’
select *from student where sname like '蓝莓' ;
使用like关键字来匹配带有通配符 ‘%’ 的字符串 ‘李%’
select from student where sname like '李%';
使用like关键字来匹配带有通配符 ‘_’ 的字符串
select from student where sname like '李__';
使用 NOT LIKE 关键字来查询不是姓李的所有人的记录
select from student where sname not like '李%';
高级查询
1.分组查询
GROUP BY 关键字可以将查询结果按某个字段或者多个字段进行分组
按student 表的ssex 字段进行分组查询
select * from student group by ssex;
按student 表的ssex 字段进行分组查询,然后显示记录大于或等于10的分组(cuunt()用来统计记录的条数)
select ssex,count(ssex) from student group by ssex having count(ssex)>=10;
2.对查询结果进行排序
查询student表中的所有记录,按照zno字段进行排序
select * from student order by zno;
查询student表中的所有记录,按照zno字段的升序排序和sno字段的降序方式进行排序
select * from student order by zno asc,sno desc;
3.限制查询结果的数量
在student表中查询从第3名同学开始的3位学生的信息
select * from student order by sno limit 2,3;
4.聚合函数
count(), sum(), avg(), max(), min()
1.使用count函数统计student表的记录数
select count(*) as '学生总人数' from student;
使用count函数统计student表不同zno值得记录值,count函数与group by 关键字一起使用
select zno,count(*) as '专业人数';
2.sum函数
使用sum函数统计sc表中学号为1414855328 的同学的总成绩
select sno sum(grade) from sc where sno='1414855328';
将sc表按照sno字段进行分组,然后使用sum函数统计各分组的总成绩
select sno,sum(grade) from sc group by sno;
3.avg函数
使用avg函数计算sc表中平均成绩
select avg(grade) from sc;
使用avg函数计算sc表中不同科目的平均成绩
select cno,avg(grade) from sc group by cno;
4.max函数
使用max 函数查询sc表中不同科目的最高成绩
select sno,cno,max(grade) frmo sc group by cno;
使用max函数查询student表中sname字段的最大值
select max(sname) from student;
5.min函数
使用min函数查询sc表中不同科目的最低成绩
select cno,min(grade) from sc group by cno;
多表查询
在具体应用中,如果需要实现多表数据记录查询,一般不使用连接查询,因为该操作效率比较低,于是MySQL有提供了连接查询的替代操作———子查询操纵。
子查询操作
**有时候,当进行查询时,需要的条件是另外一个select语句的结果,此时就要用到子查询。**
子查询中可能包括 in , not in, any , exists , not exists
等关键字。
1.子查询——in
in关键字可以判断某个字段的值是否在指定的集合中,如果字段的值在集合中,则满足查询条件,该记录将被查询出来,如果不在集合中,则不满足查询条件。
查询还没选修过任何课程的student的记录
select * from student where sno not in(select sno from sc);
查询选修过课程的student的记录
select * from student where sno in(select sno from sc);
查询和学号为108,101 的同学同年出生的所有学生的sno ,sname ,和sbirth列
select * from student where year(sbrith)
in(select year(sbrith) from student where sno in(108,101));
2.子查询——exists
exists 关键字表示存在,使用exists关键字时,内查询语句不返回查询的记录,而是返回一个真假值。如果内层查询语句查询到满足条件的记录,就会返回一个真值true 否则返回false 。当返回true时 外查询进行查询,否则外查询不进行查询。
如果存在 “金融” 这个专业,就查询所有的课程信息
select * from course where exists(select * from specialty where zname='金融');
3.子查询——any
any关键字标是满足其中任何一个条件,使用any关键字,只要满足内查询语句返回结果的一个,就可以通过该条件来执行外层查询语句。
查询比2001班级某一个同学年龄小的学生的姓名和年龄。
select sname
(date_format(from_days(to_days(now()-to_days(sbrith)),'%Y')+0)
as '年龄' from student
where sbrith > any(select sbrith from student
where sclass='计算2001')
4.子查询——all
all关键字表示满足所有的条件。使用all关键字时,只有满足内层查询语句返回的所有结果,才能执行外层的查询语句。>all 表示大于所有的值;<all 表示小于所有的值。
查询比2001班级所有同学年龄都大的学生的姓名和年龄。
select sname
(date_format(from_days(to_days(now()-to_days(sbrith)),'%Y')+0)
as '年龄' from student
where sbrith < all(select sbrith from student
where sclass='计算2001')
最后
***勤加练习,勿要眼高手低 ***