①、查询语句可以分为:
select 子句、from 子句、where 子句、order by 子句、group by 子句;
select 子句指明需要查询的项目,一般是列名,也可以是表达式;
from 子句指明被查询的表或视图名;
※※※ select 和 from 是每个SQL查询语句所必须的,其他子句是任选的。
where 子句说明查询的条件,group by 子句用于将结果分组,order by 子句将结果排序。
===================================================
②、为了方便查询,这里向新建几个表,并插入几条数据;
语句如下:
//学生表
create table students(
sno char(8) not null unique,
sname char(8) not null,
ssex char(2),
sage smallint,
sdept char(20)
);
insert into students values ('0406321', '李明', '男', 21, 'computer');
insert into students values ('0406322', '王雪', '女', 20, 'english');
insert into students values ('0406323', '程丽', '女', 22, 'math');
insert into students values ('0406327', '张三', '男', 25, 'computer');
//科目表
create table course(
cno char(8) not null unique,
cname char(8) not null,
ccredit smallint
);
insert into course values ('C001', 'English', 3);
insert into course values ('C002', 'C++', 2);
insert into course values ('C003', 'Database', 3);
//学生选课表
create table sc(
sno char(8) not null,
cno char(8) not null,
grade smallint
);
insert into sc values ('0406321', 'C001', 86);
insert into sc values ('0406321', 'C002', 90);
insert into sc values ('0406322', 'C001', 90);
insert into sc values ('0406322', 'C003', 76);
insert into sc values ('0406323', 'C001', 89);
insert into sc values ('0406323', 'C003', 70);
insert into sc values ('0406327', 'C001', null);
===================================================
③、select 和 from 子句:
◆ 查询全体学生的学号和系别:select sno, sdept from students;
◆ 查询全部列可以指定为 *,查询课程的详细记录:select * from course;
◆ 列表达式可以是列名也可以是表达式,如,要查询学生的姓名和出生年份:
select sname, 2013-sage from students;
◆ 查询结果中不允许出现重复项,select 查询时加上 distinct 选项:
select distinct cno from sc;
===================================================
④、where 子句定义查询条件:
前面示例中的查询只是对列进行了筛选,结果中包含了所有的行,而实际操作中,
不仅要对列进行筛选,行也要满足指定的条件,进行筛选,所以要用到where子句。
where 子句的后面常常加上条件表达式,如下:
◆ 比较:=, >, <, <=, >=, <>, !>, !<, not+上述比较运算符;
查询年龄大于20岁的学生姓名和系别:select sname, sdept from students where sage > 20;
◆ 确定范围:between and, not between and;
查询成绩在 80 到 90 分之间的学生的学号和成绩:select sno, grade from sc where grade between 80 and 90;
◆ 确定集合:in, not in;
查询数学系和英语系学生的姓名和系别:select sname, sdept from students where sdept in ('english', 'math');
◆ 字符匹配:like, not like;
like 引入了两个特殊意义的通配符:_(下划线) 和 %(百分号),
下划线表示任意单字符(一个汉字占用两个字节,所以为2个下划线),百分号表示长度任意的字符串。
如果是完整的字符串,则可以使用比较符号 = 取代like
查询姓氏为李的学生的信息:select * from students where sname like '李%';
◆ 空值:is null, is not null;
例如,某些学生选修课程后没有参加考试,所以有选课记录,但是没有考试成绩。
查询缺少成绩的学生的学号和课程号:select sno, cno from sc where grade is null;
◆ 多重条件:and, or;
and 和 or 可以用于连接多个查询条件:
查询年龄大于20并且是男生的学生的信息:select * from students where sage > 20 and ssex = '男';
===================================================
⑤、group by 子句对查询结果分组:
SQL聚集函数:avg,max,min,sum,count;
求所有选课的平均成绩:select avg(grade) from sc;
求每一门课程的平均成绩:select cno, avg(grade) from sc group by cno;
===================================================
⑥、order by 子句对查询结果排序:
对查询结果中按照指定的列进行排序,asc表示升序,desc表示降序,缺省表示升序;
查询学生选课表,给出选了C001号课程的学生的学号和成绩,结果以成绩升序排列:
select sno, grade from sc where cno='C001' and grade is not null order by grade asc;
网友评论