1.在查询中过滤行!
select * from employees --查询employees表(员工表)的所有列的所有数据(这里没有加where条件,所以是查询所有行,*号表示显示所有的列)
select employee_id, first_name, last_name from employees where employee_id > 200 --查询employee_id 大于 200的行数据,这次这里没有用*号,而是直接写的列名,表示只显示employee_id, first_name, last_name这3列
select salary, employee_id, first_name, last_name from employees where salary > 8888 -- 查询工资大于8888的行数据
select salary, employee_id, first_name, last_name from employees where department_id = 90 --查询在部门编号是90的员工
以上语句都是过滤筛选行数据!
注意点:
1.字符和日期需要包含在单引号之内,字符大小写敏感,日期格式敏感!
select salary, employee_id, first_name, last_name from employees where first_name = 'Allan' --(需要注意:我们说oracle数据库中不区分大小写,指的是select ,列名,表名,from,where等等等这些不区分大小写,但是条件值或者说字段值等等这些是严格区分大小写的,比如这里的first_name = 'Allan' ,这里的字段值=Allan,这样是可以查询到一条数据,但是你写成first_name = 'allan',这样就查询不到数据了,所以字段值是严格区分大小写的)
select hire_date, employee_id, first_name, last_name from employees where hire_date = '1999/3/17'; --报错ORA-01861: 文字与格式字符串不匹配(这个日期格式确实挺恶心的)
select hire_date, employee_id, first_name, last_name from employees where hire_date = '17-3月-1999'; --必须按照这种日期格式来写,才正确(确实也挺恶心的)
所以,以后我们一般都是用下面的这种写法
select hire_date, employee_id, first_name, last_name from employees where to_char(hire_date,'yyyy-mm-dd') = '1999-03-17'
知识点:比较运算
= 等于
> 大于
>= 大于等于
< 小于
<= 小于等于
<> 不等于(或者也可以使用!=)
:=符号 赋值使用(在plsql中会使用到,给变量赋值的时候会使用到该符号)
select salary, employee_id, first_name, last_name from employees where salary >= 17000 --查询工资大于17000的员工,包含17000
select salary, employee_id, first_name, last_name from employees where salary >= 17000 and salary < 24000 --查询工资大于17000(包含17000)并且工资小于24000(不包含24000)的员工
其他比较运算符
between and (在2个值之间,包含边界)
in(值列表) 你可以把你想要的值 列出来
like 模糊查询
is null 和 is not null 空值
select salary, employee_id, first_name, last_name from employees where salary between 17000 and 24000 --包含17000和24000
select department_id, employee_id, last_name from employees where department_id = 70 or department_id = 80 or department_id = 90 --查询部门编号是70,80,90的员工
也可以是下面的这种写法,使用in的写法也可以!
select department_id, employee_id, last_name from employees where department_id in (70, 80, 90)
注意点(后面我们会讲到order by排序): where要和from挨着(where要紧跟着from),where和from比较亲近,order by 不能放在from 和 where 之间的位置!
% 百分号 (表示0个或多个字符,任意个字符)
_ 下划线 (代表一个字符)
select department_id, employee_id, last_name from employees where last_name like '%a%' --查询名字包含a的员工
select department_id, employee_id, last_name from employees where last_name like '%a' --查询名字末位是a的员工
select department_id, employee_id, last_name from employees where last_name like '_a%' --查询名字第2位是a的员工
select department_id, employee_id, last_name from employees where last_name like '__a%' --查询名字第3位是a的员工
select last_name from employees where last_name like '%\_%' escape '\' -- 查询名字中含有下划线的员工(在oracle中_下划线表示任意某个字符(1个字符),所以需要使用到escape关键字,转义字符) 如下面这样写也可以
select last_name from employees where last_name like '%#_%' escape '#'
使用转义符。例如:将[%]转为[\%]、[_]转为[\_],然后再加上[ESCAPE ‘\’] 即可
select last_name, commission_pct from employees where commission_pct is null --查询commission_pct值是null的员工
select last_name, commission_pct from employees where commission_pct is not null --查询commission_pct值是非null的员工
逻辑运算
and 逻辑并
select last_name, department_id, salary from employees where department_id in (80,90) and salary > 13000; --查询部门编号是80和90并且工资大于13000的员工
select last_name, department_id, salary from employees where department_id not in (80) and salary > 13000;
or 逻辑或
not 逻辑否
2.在查询中对行进行排序(order by)!
select last_name, department_id, salary from employees where department_id in (80) order by salary desc --查询部门编号是80的员工,并且工资按照从高往低的顺序
select last_name, department_id, salary from employees where department_id in (80) order by salary asc --查询部门编号是80的员工,并且工资按照从低往高的顺序
select last_name, department_id, salary from employees where department_id in (80) order by salary -- 默认是asc升序(默认是从小到大)
select last_name, department_id, salary from employees where department_id in (80) order by salary asc, last_name asc -- 两层排序,如果salary排序完后,salary工资一样,那就再一次按照last_name来排序
select last_name, department_id, salary, salary * 12 as annum_salary from employees where department_id in (80) order by annum_salary --按照别名来排序