你的位置:首页 > 软件开发 > 数据库 > Oracle 数据库二 基本查询

Oracle 数据库二 基本查询

发布时间:2016-11-08 10:03:38
查询当前用户:show user查看当前用户下的表:select *from tab;设置行宽: show linesize;(查看行宽) set linesize 120;(设置行宽)设置列宽:col 列名 for a8; (设置字符串) col 列名 f ...

查询当前用户:show user

查看当前用户下的表:select *from tab;

设置行宽: show linesize;(查看行宽)     set linesize 120;(设置行宽)

设置列宽:col 列名 for a8; (设置字符串)  col 列名 for 9999;(设置数字的)

查看表的结构:dese emp;

查询所有的员工信息:select * from emp;

投影:select empno,ename,sal from emp;

滤空函数:select empno,ename,(sal*12+nvl(comm,0)) from emp;

取别名的三种方式:

as "别名"

"别名"

别名  ——不能用关键字

去重:distinct

select distinct (列) from emp;

select distince 列,列 from emp;

字符串的连接

select concat('hello','world') from emp;

select 'hello'||'world' from emp;

可用伪表来测试:dual;

where的应用:

  查询10号部门的员工:

  select * from emp where deptno=10;

  查询名字叫KING的员工

  select * from emp where ename='KING';

修改日期格式:

select * from v$nls_parameters;(查询日期格式)  alter session/system set NLS_DATE_FORMAT='yyyy-mm-dd'; (一般修改当前会话session)

between...and...  在...之间

in/not in

is null 为空的

is not null 不为空的

模糊查询:

select * from emp

where job like'%H%' 包含了H的 / 'H%' 以H开头 / '%H' 以H结尾的 / '_H%' 第二位为H的 / '%\_%'escape'\' 找带有_的;

commit 提交

rollback 事务回滚

a两个空格;  语句后追加

排序:

  order by 默认升序(asc) + desc降序

  order by +列、表达式、别名、序号

  order by 字段1,字段2;   先用字段1来排序,其中相同的再用字段2来排

null 是oracle中的最大值

null last 让空值最后显示

 

字符函数:

upper  小写转换大写

cower  大写转换小写

initcap  首字母大写

//select upper('hello') 大写,lower('HELLO') 小写,initcap('hello world') 首字母大写 from dual;

length  获取字符的长度

//select length('hello') 长度,lengthb('hello') from dual;

lengthb  获取字节的长度

//select length('中') 长度1,lengthb('中') 长度2 from dual;

replace  替换

//select replace('hello world','l','a') from dual;

substr  截取

//select substr('hello world',2,4) from dual;

instr  查找字符串中某个字符的位置

//select instr('hello world','o') from dual;

lpad  左补齐

//select lpad(999,10,'*') from dual;      *******999

rpad  右补齐

trim  去除前后的某个字符或字符串

select trim('h' from 'hello world') from dual;

 

数值函数:

round  四舍五入小数

//select round(45.926,2) 数据1,round(45.926,1) 数据2,round(45.926,0) 数据三,round(45.926,-1) 数据4, round(45.926,-2) 数据5 from dual;

trunc  截断小数

//select trunc(45.926,2) 数据1,trunc(45.926,1) 数据2,trunc(45.926,0) 数据三, trunc(45.926,-1) 数据4, trunc(45.926,-2) 数据5 from dual;

mod  除数取余

//select mod(10,3) from dual;

 

日期函数:

sysdate  今天

sysdate+1  明天

sysdate-1  昨天

// select sysdate - (sysdate -1) from dual;  今天减去昨天

last_day  某一月的最后一天

//select last_day(sysdate) from dual;

next_day  获取下个星期几

//next_day(sysdate,"星期三")

add_moths  追加几个月

//add_months(sysdate,3)

months_beween  精确计算月份

//months_beween(sysdate.hiredate)

round  四舍五入日期

//select round(sysdate,'year') from dual;

truuc  截断日期

//select round(sysdate,'month') from dual;

 

转换函数:

to_char()  将其他类型的数据转换为字符串

//select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')from dual;

//select to_char(12535,'L99,999') from dual;

to_date:  将其他格式的数据转换为日期格式

//select to-date(sydate) from dual;

to_number  将其他格式的数据转换为数字

//select to_number('999') from dual;

 

通用函数:

nvl: 虑空函数

nvl2: 是nvl函数的一个加强版 nvl2(a,b,c) 如果a是空的则返回c,否则返回b

//select comm,nvl2(comm,comm,100) from emp;

nullif  nullif(a,b) 如果a等于b则返回空,否则返回a

//select nullif('hello','world') from dual;

coalesce(a,b,c...)  从左往右一次查找第一个不为空的值返回

涨工资:

第一种方式:case....end   SQL99语句

select job, sal,

case

when job = 'SALESMAN',sal+800

when job = 'MANAGER' then sal+1000

when job = 'CLERK' then sal + 900

else sal+400

end

from emp;

第二种方式:用decode  Oracle中独有的

select job,sal,decode(job,'SALESMAN',sal+800,'MANAGER',sal+1000,'CLERK',sal+900,sal+400) from emp;

 

多行函数

多行函数也称组函数,自带滤空功能

nvl(com ,0)  取消滤空

sum  求和

count(*)  统计

//select count(empno) from emp;

avg  平均值

max  最大值

min  最小值

group by  分组

//select sum(sal) ,avg(nvl(sal,0))

deptno from emp

group by;//

查询部门平均工资大于2000的部门编号:

//select deptno,avg(sal) 平均工资

  from emp

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:Oracle 数据库二 基本查询

关键词:数据库

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。