您的位置:首页 > 数据库 > Oracle

oracle查询(where,order by)相关命令

2017-03-07 10:59 495 查看
此文章使用oracle自带的emp表进行举例

对emp表的格式进行调整  a表示字符型 ,9表示数字型

完整的格式:

column empno format 9999;

简写格式:

col empno for 9999;

col ename for a10;

col job for a10;

col mgr for 9999;   

col hiredate for a12;

col sal for 9999;

col comm for 9999;

col deptno for 99;

set pagesize 20;

col tname for a20;

基本的select ... from table_name查询

查询表结构 desc emp;

复制emp的结构和数据到新表中new_emp

create table new_emp

as

select * from emp;

#查询拷贝的新表

select * from new_emp;

查询emp表的所有内容

selec * from emp;

查询emp表的不重复工作

select distinct job from emp;

查询员工的编号,姓名,月薪, 年薪

select empno 编号,ename 姓名,sal 月薪,sal*12 年薪 from emp;

查询员工的编号,姓名,月薪,年薪,年收入(月薪加奖金)

select empno 编号,ename 姓名,sal 月薪,sal*12 年薪,sal*12+NVL(comm,0) 年收入 from emp;
注:NVL(comm,0)意思是奖金为null时让奖金为0

使用别名查询员工的编号,姓名,月薪

select empno 编号,ename 姓名,sal 月薪 from emp;

带有where条件的筛选查询

查询emp表中部门号(deptno)为20员工的信息

select * from emp where deptno=20;

查询姓名(ename)为SMITH的员工信息

select * from emp where ename='SMITH';

查询薪水在1300-1600之间的员工,包括1300和1600

select * from emp where sal>=1300 and sal<=1600;

select * from emp where sal between 1300
and 1600;

查询薪水不在1300-1600之间的员工,包括1300和1600

select * from emp where sal not between 1300
and 1600;

查询入职时间在“1981-2月-20”到“1982-1月-23”之间的员工

select * from emp where hiredate between '20-2月-81' and '23-1月-82';

注意:

1)对于数值型,小数值在前,大数值在后

2)对于日期型,年长值在前,年小值在后

查找以大写字母S开头的员工信息

select * from emp where ename like 'S%';

注意: =    是精确查找

           like 是模糊查找

           %    通配符

            _     通配一个字符

       

查询员工姓名含有‘_’的信息,使用\转义符,让其后的字符回归本意

#先插入一条数据

insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno)

values(6666,'t_t','clerk',7902,'9-9月-16',900,0,20);

#查询结果应该为上面这条信息

select * from emp where ename like '%\_%' escape '\';

插入一个姓名叫 ' 的员工

insert into emp(empno,ename) values(2222,'''');

插入一个姓名叫 '' 的员工

insert into emp(empno,ename) values(6666,'''''');

查询无佣金且工资大于1500的员工

select *

from emp

where (comm is null) and (sal>1500);

查询工资是1500或3000或5000的员工

select *

from emp

where sal in (1500,3000,5000);

查询职位是"MANAGER"或职位不是"ANALYST"的员工(方式一,使用!=或<>)

select *

from emp

where (job='MANAGER') or (job<>'ANALYST');

等价于

select *

from emp

where (job='MANAGER') or not(job='ANALYST');

带有 order by 的结果集排序查询

order by后面可以跟列名、表达式、列号(从1开始,在select子句中的列号)

order bydesc默认升序排列asc,降序排列为desc

查询员工信息(编号,姓名,月薪,年薪),按月薪升序排序,默认升序,如果月薪相同,按oracle内置的校验规则排序

select empno,ename,sal,sal*12 salOfYear from emp

order by sal asc;

查询员工信息(编号,姓名,月薪,年薪),按月薪降序排序

select empno,ename,sal,sal*12

from emp

order by sal desc;

根据别名对年薪进行排序

select empno,ename,sal,hiredate,sal*12 "年薪"

from emp

order by "年薪" desc;

根据表达式对年薪进行排序

select empno,ename,sal,hiredate,sal*12 "年薪"

from emp

order by sal*12 desc;

根据列号对年薪进行排序,列号从1开始计数

select empno,ename,sal,hiredate,sal*12 "年薪"

from emp

order by 5 desc;

注:5代表sal*12这个表达式

查询员工信息,按佣金升序或降序排列,null值看成最大值

select * from emp order by comm desc;

查询员工信息,对有佣金的员工,按佣金降序排列,当order by 和 where 同时出现时,order by 在最后

select *

from emp

where comm is not null

order by comm desc;

查询员工信息,按工资降序排列,相同工资的员工再按入职时间降序排列

select *

from emp

order by sal desc,hiredate desc;

查询20号部门,且工资大于1500,按入职时间降序排列

select *

from emp

where (deptno=20) and (sal>1500)

order by hiredate desc;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据库 oracle