·新学习·新旅程 / NEW TERM
Oracle学习笔记_2_条件查询
条件查询
条件查询需要用到 where 语句,where 必须放到 from 语句表的后面。
支持的运算符
运算符 | 说明 |
< | 小于 |
> | 大于 |
<= | 小于等于 |
>= | 大于等于 |
= | 等于 |
!= 或 <> | 不等于 |
between···and··· | 两个值之间 |
is null | 为null |
is not null | 不为null |
基础运算符
# >、=、<、<=、>=、<>、!=
这七个运算符操作相似
-- 查询stu表中年龄为18的人的信息
select * from stu where age = 18;
-- 查询stu表中姓名不为tom的人的信息
select * from stu where name <> 'tom';
在 sql 语句中如果是字符串采用单引号引起来,如果是数值型也可以引起来,只不过是数值类型数据当成字符串来处理。
BETWEEN···AND···
# between···and··· 在**于**之间
-- 查询stu表中年龄在15至20之间的人的信息
select * from stu where age between 15 and 20 ;
等同于
select * from stu where age >= 15 and age <= 20;
含最大值和最小值 ,不仅仅可以应用在数值类型的数据上,还可以应用在字符数据类型上 ,对于两个参数的设定是有限制的,小的数在前,大的数在后。
IS (not) NULL
# is null 为null
-- 查询stu表中年龄为null的人的信息
select * from stu where age is null;
# is not null 不为null
-- 查询stu表中年龄不为null的人的信息
select * from stu where age is not null;
null 为空,但不是空串,为 null 可以设置这个字段不同填值,如果查询为 null 的字段,采用is null,如果查询不为null的字段,采用is not null。
AND
# and 与,和,并且
-- 查询stu表中年龄为10,姓名为tom的人的信息
select * from stu where age = 10 and name = 'tom';
多个条件之间必须全部满足
OR
# or 或
-- 查询stu表中年龄为10或15的人的信息
select * from stu where age = 10 or age = 15;
多个条件只要有一个满足即可
运算符优先级
# 比较运算符 > not > and > or
可以使用()来改变运算顺序
注意:
1、比较运算符中乘除的优先级高于加减;
2、同一优先级运算符从左向右执行;
3、括号内的运算先执行
关于运算符的问题:不用记,没有把握尽量采用括号
IN (忽略空值)
# in 包含,完全跟or一样,不过用in会更简洁
-- 查询stu表中年龄为10或15的人的信息
select * from stu where age = 10 or age = 15;
使用in书写
select * from stu where age in (10,15);
in括号里面如果是字符则用单引号引起来
如:in('tom','lim')
NOT IN (不忽略空值)
# not in 不等于,类似 <>或!=
-- 查询stu表中年龄不是10、15的人的信息
select * from stu where age != 10 and age <> 15;
使用not in 书写
select * from stu where age not in(10,15);
LIKE 模糊查询
# like 模糊查询,使用%或_进行字符代替
% 任意数量字符 _ 一个字符
-- 查询stu表中名字带有 't' 的人的信息
select * from where name like ('%t%');
-- 查询stu表中名字的第二个字符为 'e' 的人的信息
select * from where name like '_e%' ;
# 转义字符 escape 用来查询有%或 _
-- 查询stu表中名字带有%的人的信息
select * from where name like '%@%%' escape '@';
escape 后面确定的是哪个符号,那这个符号就代表是转义字符
Like 语句是可以应用在数值类型的数据上的,但是如果没有使用引号括起来的话,那么不能使用%和下划线。类似于等号的操作,如果使用引号括起来的话,那么可以使用%和下划线,将数值类型的数据转换为字符类型后进行处理。
简单的条件查询语句记录
2021/9/13




