数据查询是数据库的核心操作,在数据库应用中,select语句提供了丰富的查询能力,可以查询一个表或多个表;对查询列进行筛选、计算;对查询进行分组、排序;甚至可以在一个select语句中嵌套另一个select语句,语法格式如下:
SELECT [ALL|DISTINCT] <目标列表达式>[,<目标列表达式>] …
FROM <表名或视图名>[,<表名或视图名> ]…|(SELECT 语句)
[AS]<别名>
[ WHERE <条件表达式> ]
[ GROUP BY <列名1> [ HAVING <条件表达式> ] ]
[ ORDER BY <列名2> [ ASC|DESC ] ];
在以后的数据查询所用到的表(此表中的数据没写全)都是以下三个表:
select column_name [,column_name...]
from table_name
例1、 在student表中查询全体学生的姓名、学号、所在院系:
select sname,sno,sdept
from student
注意:在指定列查询中,列的显示顺序有select子句指定,与数据在表中的存储顺序无关;同时,在查询多列时,用“,”将各个字段隔开。
select * from <表名>
注:使用“ * ”通配符时要慎重,在不需要查询所有列时,尽量采用前面介绍的指定列查询,以免占用过多的资源。
select column_name as new_name[,...n]
from table_name
select column_name new_name[,...n]
from table_name
select new_name = column_name[,...n]
from table_name
例2、 查询数据表student中所有学生的学号sno及年龄sage,结果中各列的标题分别指定为学号、年龄:
select sno as 学号,sage as 年龄
from student
或者
select sno 学号,sage 年龄
from student
或者
student 学号 = sno,年龄 = sage
from student
注意:当自定义的标题中含有空格时,必须用单引号将标题括起来。
case
when 条件1 then 表达式1
when 条件2 then 表达式2
...
else 表达式
end
例3、 查询数据表SC的学生的学号、课程号及成绩,对于成绩按以下规定显示:成绩在60分以下显示为不及格;60-79之间显示为良好;80以上显示为优秀。
select sno,cno,grade=
case
when grade<60 then ‘不及格’
when grade>=60 and grade<=79 then ‘良好’
when grade>=80 then ‘优秀’
end
from sc
例4、 在student表中查询全体学生的姓名、出生年份和所在的院系,要求用小写字母表示系名。
select sname,year(getdata())-sage,lower(sdept)
from student
select distinct column_name [,column_name...]
from <表名>
注:关键字distinct的含义是对结果集中的重复行只选择一个,保证行的唯一性。
select top n [percent] column_name[,column_name]
from <表名>
注:n 是一个正整数,表示返回查询结果集的前n行。若带percent关键字,则表示返回结果集的前n%行。
例5、 显示student表中的学生前5条记录,并显示其学号、姓名
select top 5 sno,sname
from student
select column_name from <表名>
where 条件
常见的查询条件
查询条件 | 谓词 |
---|---|
比较 | =, >, <, >=, <=, !=, <>, !>, !<, not+上述比较运算符 |
确定范围 | between and, not between and |
确定集合 | in, not in |
字符匹配 | like, not like |
空值 | is null, not is null |
多重条件(逻辑运算) | and, or, not |
例6、 查询sc表考试成绩有不及格的学生的学号
select distinct sno
from sc
where grade < 60
例7、 查询年龄不在20~30岁之间的学生姓名、系别和年龄
select sname, sdept, sage
from student
where sage not between 20 and 30;
例8、 在student查询既不是计算机学科系、数学系,也不是信息系的学生的姓名和性别。
select sname,ssex
from student
where sdept not in ('IS', 'MA','CS')
例9、 在student表中查询院系为CS或者为IS的男学生的记录
select * from student
where sdept in ('CS','IS') and ssex = '男'
或者
select * from student
where sdept = 'CS' or sdept = 'IS' or ssex = '男'
通配符 | 含义 |
---|---|
_下划线 | 任何单个字符:(如a_c表示以a开头c结尾,长度为3的字符串) |
%百分号 | 包含0个或多个字符的任意字符串:(如a%c表示以a开头c结尾,长度为任意长度的字符串) |
[ ] | 指定范围:(如[ a-f ]或者[ abcdef ]表示a-f内的任意单个字符) |
[ ^ ] | 不在指定范围:(如[ ^abcdef] 表示不在abcdef内的任意单个字符) |
上表事例:
like ‘王%’ | 搜索以“王”开头的所有字符串。如“王三”,“王大千”等 |
---|---|
like ‘%三’ | 搜索以“三”结尾的所有字符串。如“王三”,“王二三”等 |
like ‘%三%’ | 搜索任何位置包含“三”的所有字符串。如“王三”,“王三千”等 |
like ‘_abc’ | 搜索以“abc”结尾的4个字母的信息 |
like ‘[a-f]st’ | 搜索以“st”结尾、以a~f的任意一个字母开头的信息 |
like 'm [ ^ab]% | 搜索以“m”开头,并且第二个字母不是a,b的信息 |
例10、 在student表中查询学号为201215121学生的详细情况
select * from student
where sno like '201215121'
等价于:
select * from student
where sno = '201215121'
注:如果like后面的匹配串中不含通配符,则可以用=(等于)运算符取代like谓词,用!=或者<>(不等于)运算符取代not like谓词。
例11、 在student表中查询姓“欧阳”且全名为三个汉字的学生的姓名。
select sname from student
where sname like '欧阳_'
escape定义转义符:
在使用LIKE进行模糊查询时,当“%”、“_”和“[]”符号单独出现时,都会被作为通配符进行处理。但有时可能需要搜索的字符串包含一个或多个特殊通配符。若要搜索作为字符而不是通配符的百分号,必须提供ESCAPE关键字和转义符。
如:like ‘%b%’ escape ‘b’,‘b’就作为了转义字符,用来将第二个%转为普通。
例12、 查询以“DB_”开头,且倒数第3个字符为i的课程的详细情况。
select * from course
where cname like 'DB\_%i__' escape '\'
例13、 在sc表中某些学生选修课程后面没有参加考试,所以有选课记录,但没有考试成绩。查询缺少成绩的学生的学号和相应的课程号。
select sno,cno from sc
where grade is null
例14、 在student表中查询计算机系统在20岁以下的学生姓名
select sname from student
where sdept = 'CS' and sage < 20
练习:
1.
select * from student
where sno like ‘2002%’
2.
select * from student
where sno like ‘____5%’
3.
select * from student
where sno like ‘____[^5]%’
4.
select sno,sname,ssex,sdept from student
where sno like '%[1,2,3]'