一、语法
select 查询列表 ①
from 表名 ②
where 筛选条件 ③
如果条件成立,数据显示在结果集中,否则不显示。
执行顺序:先执行②也就是先查库中的表,在执行where筛选条件,最后执行①走select
二、根据筛选条件进行分类
1、按条件表达式筛选
条件运算符:> < = !=(<>) >= <=
eg:查询部分编号不等于80的员工名和薪水
select
last_name,salary
from
employees
where department_id <> 80;
2、按逻辑表达式筛选
用于连接条件表达式
&& and :两个条件都为true,结果为true,反之为false
|| or :只要一个条件为true,结果为true,反之为false
not ! : 如果连接条件本身为false.结果为true,反之为false
eg:查询部分标号不是在90到100之间,或者工资高于15000的员工信息
select
*
from
employees
where not(department_id=90 and department_id=100) || salary >15000;
或者
where department_id >=90 or department_id <= 100 || salary>15000;
3、模糊查询
like
注:一般搭配通配符使用,可以判断字符型或数值型
常用通配符
%:任意多个字符,包含0个字符
_:任意单个字符
eg:查询员工名中包含字符a的员工信息
select *
from
employees
where last_name like '%a';
eg:查询员工名中第三个字符为n,第五个字符为l的员工名和部门
select
last_name , department_id
from
employees
where last_name like '__n_l%';
eg:查询员工名第二个字符为_的员工名
select
last_name
from
employees
where last_name like '_\_%';
或者
where last_name like '_$_% Escape '$';
select
*
from
employees
where department_id like '1__';
between and
注:使用between and 提高语句的简洁性
包含临界值
两个临界值不要换顺序
eg:员工编号100到200之间的信息
select * from employess
wherre department_id between 100 and 200;
in
注:用于判断某字段的值是否属于in 列表中某一项
特点
①提到简洁性
②in列表的值类型必须统一或者兼容
③不支持通配符 _ %
eg:查询员工种编号是IT_PROG,AP_VP,AD_OP中的一个员工名和工种编号
select last_name,job_id
from
employees
where job_id IN (IT_PROG,AP_VP,AD_OP);
is null
注:用来判断值是否为null的情况
!= <> 和= 不能用于判断null值
eg:查询没有奖金的员工名和奖金率
select last_name,commission_pct
from employees
where commission_pct is null;
is not null
is null 与<=> 安全等于 既可以判断null值,也可以判断普通类型的值
普通类型的数值 | null值 | 可读性 | |
---|---|---|---|
is null | 不支持 | 支持 | 高 |
<=> | 支持 | 支持 | 低 |