Oracle 条件查询
Oracle条件查询时经常使用=、IN、LIKE、BETWEEN…AND来作为条件查询的操作符。
= 操作符
在条件查询语句中“=”表示列值等于一个固定值所查询出的结果。
案例1、查询学生成绩表“score”中课程id为“R20180101”,成绩为“85”分的同学信息:
select t.stuid,
t.courseid,
t.score,
b.stuname,
b.sex,
b.age,
b.classno,
b.grade
from score t, stuinfo b
where t.stuid = b.stuid
and t.courseid = 'R20180101'
and t.score = '85';
结果如下:
IN 操作符
在 Where 子句中可以使用 IN 操作符来查询其列值在指定的列表中的查询结果。
案例2、查询学生成绩表“score”中课程id为“R20180101”,成绩为“79”、“85”、“89”分的同学信息:
--利用逻辑运算符or 和条件"=" 查询
select t.stuid,
t.courseid,
t.score,
b.stuname,
b.sex,
b.age,
b.classno,
b.grade
from score t, stuinfo b
where t.stuid = b.stuid
and t.courseid = 'R20180101'
and (t.score = '85' or t.score ='89' or t.score ='79');
-- 利用Oracle操作符”IN“查询
select t.stuid,
t.courseid,
t.score,
b.stuname,
b.sex,
b.age,
b.classno,
b.grade
from score t, stuinfo b
where t.stuid = b.stuid
and t.courseid = 'R20180101'
and t.score in ('85','89','79');
结果如下:
BETWEEN…AND
在 WHERE 子句中,可以使用 BETWEEN…AND 操作符来查询列值包含在指定区间内的查询结果 。
案例3、查询学生成绩表“score”中课程id为“R20180101”,成绩在70-95之间的学生信息:
select t.stuid,
t.courseid,
t.score,
b.stuname,
b.sex,
b.age,
b.classno,
b.grade
from score t, stuinfo b
where t.stuid = b.stuid
and t.courseid = 'R20180101'
and t.score between '70' and '95';
结果如下:
LIKE 模糊查询
在Oracle条件查询where条件之中,当遇到查询值不清楚时,可以利用模糊查询LIKE关键字进行where条件的模糊查询。
LIKE 关键字通过字符匹配检索出所需要的数据行。
字符匹配操作可以使用通配符“%”和“_” :
-
1、%:表示零个或者多个任意字符。
-
2、_:代表一个任意字符。
-
3、\:指转义字符,“\%”在字符串中表示一个字符“%”。
案例4、查询学生基本信息表“STUINFO”中姓“张”的学生基本信息:
select * from STUINFO t where t.stuname like '张%';
结果如下:
案例5、查询学生基本信息表“STUINFO”中姓“张”的,并且姓名长度是两个字的学生基本信息:
select * from STUINFO t where t.stuname like '张_';
结果如下: