select 排序
查询员工信息(编号,姓名,月薪,年薪),按月薪升序排序,默认升序,如果月薪相同,按oracle内置的校验规则排序select empno,ename,sal,sal*12
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 hiredate desc;
order by后面可以跟列名、别名、表达式、列号(从1开始,在select子句中的列号)列名:
select empno,ename,sal,hiredate,sal*12 "年薪"
from emp
order by hiredate 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;
查询员工信息,按佣金升序或降序排列,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;
select *
from emp
order by sal desc,hiredate asc;注意:只有当sal相同的情况下,hiredate排序才有作用
查询20号部门,且工资大于1500,按入职时间降序排列select *
from emp
where (deptno=20) and (sal>1500)
order by hiredate desc;
oracle 三大类型转换
oracle 中三大类型与隐式数据类型转换varchar2 变长/char定长–>number,例如:’123’->123
varchar2/char–>date,例如:’25-4月-15’->’25-4月-15’
number—->varchar2/char,例如:123->’123’
date——>varchar2/char,例如:’25-4月-15’->’25-4月-15’
oracle如何隐式转换:=号二边的类型是否相同
如果=号二边的类型不同,尝试的去做转换
在转换时,要确保合法合理,否则转换会失败,例如:12月不会有32天,一年中不会有13月
查询1980年12月17日入职的员工(方式一:日期隐示式转换)select * from emp where hiredate = '17-12月-80';
使用to_char(日期,’格”常量”式’)函数将日期转成字符串,显示如下格式:2015 年 04 月 25 日星期六select to_char(sysdate,'yyyy" 年 "mm" 月 "dd" 日 "day') from dual;
使用to_char(日期,’格式’)函数将日期转成字符串,显示如格式:2015-04-25今天是星期15:15:15select to_char(sysdate,'yyyy-mm-dd"今天是"day hh24:mi:ss') from dual;
或
select to_char(sysdate,'yyyy-mm-dd"今天是"day HH12:MI:SS AM') from dual;
使用to_char(数值,’格式’)函数将数值转成字符串,显示如下格式:$1,234select to_char(1234,'$9,999') from dual;
使用to_char(数值,’格式’)函数将数值转成字符串,显示如下格式:¥1,234select to_char(1234,’$9,999’) from dual;select to_char(1234,'L9,999') from dual;
使用to_date(‘字符串’,’格式’)函数,查询1980年12月17日入职的员工(方式二:日期显式转换)select * from emp where hiredate = to_date('1980年12月17日','yyyy"年"mm"月"dd"日"');
或
select * from emp where hiredate = to_date('1980#12#17','yyyy"#"mm"#"dd');
或
select * from emp where hiredate = to_date('1980-12-17','yyyy-mm-dd');
使用to_number(‘字符串’)函数将字符串‘123’转成数字123select to_number('123') from dual;注意:select ‘123’ + 123 from dual;–值为 246select ‘123’ || 123 from dual;–值为123123
hogen
2017-09-09