oracle基本查询语句总结
spool E:\基本查询.txt |
将命令行的语句写入到指定的目下的指定的文件中 |
|
host cls |
清屏命令 |
|
show user |
显示当前操作的用户 |
|
desc emp |
查看表结构 |
|
select * from emp; |
查看所有的员工的信息 |
|
--设置行宽 |
SQL> --设置行宽 SQL> show linesize SQL> set linesize 150 SQL> --设置列宽 SQL> col ename for a8代表8为字符组成的字符串 SQL> col sal for 9999 代表4位的数字 |
|
SQL中的null 1包含null的表达式都为null 2. null永远!=null |
select empno,ename,sal,sal*12,comm,sal*12+nvl(comm,0) select * from emp where comm is null; 已选择 14 行。 SQL> --2. null永远!=null SQL> --查询奖金为null的员工 SQL> select * 2 from emp 3 where comm=null; 未选定行 SQL> select * 2 from emp 3 where comm is null; |
|
ed |
该命令主要用在当前一个sql语句写错时,然又不想重新写,这个时候可以用ed命令记事本调出来对先去的命令进行编辑,如果该命令用在Linux系统中,调出来的应该是vi编辑器 |
|
别名设置 |
select empno as "员工号",ename "姓名",sal "月 薪",sal*12,comm,sal*12+nvl(comm,0) 2* from emp |
|
c 命令 |
该命令会对错误的行信息进行修改示例如下: SQL> select empno,ename,sal 2 form emp; form emp * 第 2 行出现错误: ORA-00923: 未找到要求的 FROM 关键字 SQL> 2 2* form emp SQL> --c命令 change SQL> c /form/from 2* from emp SQL> / |
|
nvl(comm,0)--滤空函数 |
select empno,ename,sal,sal*12,comm,sal*12+nvl(comm,0) |
|
distinct |
去掉重复记录select deptno from emp; |
|
Distinct组合 |
select distinct deptno,job from emp; |
|
Concat |
1. select concat('Hello',' World') from dual; 2. select 'hello'||' world' 值 from dual; 3. SQL> --查询员工信息: ***的薪水是**** SQL> select ename||'的薪水是'||sal 值 2 from emp; |
|
过滤和查询 |
||
where |
select * from emp where deptno=10; 在where过滤中对大小写敏感 |
|
date |
SQL> -- 日期格式敏感 SQL> --查询入职日期是17-11月-81的员工 SQL> select * 2 from emp 3 where hiredate='17-11月-81'; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- -------- --------- ---------- -------------- ----- ---------- ---------- 7839 KING PRESIDENT 17-11月-81 5000 10 SQL> --修改日期格式 SQL> select * from v$nls_parameters; alter session set NLS_DATE_FORMAT='yyyy-mm-dd'; |
|
between and between and: . 包含边界 2. 小值在前 大值在后 |
SQL> --查询薪水1000~2000之间的员工 SQL> select * 2 from emp 3 where sal between 1000 and 2000; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- -------- --------- ---------- -------------- ----- ---------- ---------- 7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30 7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30 7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30 7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30 7876 ADAMS CLERK 7788 23-5月 -87 1100 20 7934 MILLER CLERK 7782 23-1月 -82 1300 10 |
|
in |
select * from emp where deptno in (10,20); SQL> --null 3: 如果集合中含义null,不能使用not in;但可以使用in SQL> ed 已写入 file afiedt.buf 1 select * 2 from emp 3* where deptno in (10,20,null) |
|
like % |
1.查询名字以S打头的员工 select * from emp where ename like 'S%'; 2.查询名字是4个字的员工 select * from emp where ename like '____'; 3.-查询名字中含有下划线的员工 > select * from emp where ename like '%\_%'; escape '\' |
|
order by |
排序操作order by 后面 + 列,表达式,别名,序号 order by 作用于后面所有的列,desc只作用于离他最近的一列 |
|
排序时的下技巧 |
当我们对某一类进行倒序排序的时候,null会放在最上面,这个时候为了避免前面n列都是null值,影响观察,可以采取下面的方法 select *from emp order by comm desc nulls last |
|
单行函数 |
||
Lower Uppper initcap |
select lower('Hello World') 转小写,upper('Hello World') 转大写, initcap('hello world') 首字母大写 from dual; 转小写 转大写 首字母大写 ----------- ----------- ----------- hello world HELLO WORLD Hello World |
|
substr(a,b) |
substr(a,b) 从 a中,第b位开始 select substr('Hello World',3) from dual; SUBSTR('H --------- llo World |
|
substr(a,b,c) |
SQL>substr(a,b,c) 从a中,第b位开始,取c位 SQL> select substr('Hello World',3,4) from dual; SUBS ---- llo |
|
length 字符数 lengthb字节数 |
select length('Hello World') 字符,lengthb('Hello World') 字节 from dual; 字符 字节 ---------- ---------- 11 11 select length('中国') 字符,lengthb('中国') 字节from dual 字符 字节 ---------- ---------- 2 4 |
|
-instr(a,b) 在a中,查找b |
select instr('Hello World','ll') from dual; INSTR('HELLOWORLD','LL') ------------------------ 3 |
|
lpad 左填充 rpad右填充 |
select lpad('abcd',10,'*') 左,rpad('abcd',10,'*') 右 左 右 ---------- ---------- ******abcd abcd****** |
|
trim |
去掉前后指定的字符 select trim('H' from 'Hello WorldH') from dual; TRIM('H'FR ---------- ello World |
|
replace 替换 |
select replace('Hello World','l','*') from dual; REPLACE('HE ----------- He**o Wor*d |
|
-四舍五入 |
select round(45.926,2) 一,round(45.926,1) 二,round(45.926,0) 三 round(45.926,-1) 四, round(45.926,-2) 五 from dual; 一 二 三 四 五 ---------- ---------- ---------- ---------- ---------------------------------------- 45.92 45.9 45 40 0 |
|
当前系统时间date 不允许日期 + 日期 |
select sysdate from dual; SYSDATE -------------- 17-9月 -14 select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual; select to_char(systimestamp,'yyyy-mm-dd hh24:mi:ss:ff') from dual; 员工的工龄: 天 星期 月 年 SQL> select ename,hiredate,(sysdate-hiredate) 天,(sysdate-hiredate)/7 星期, (sysdate-hiredate)/30 月,(sysdate-hiredate)/365 年 from emp; |
|
add_months |
select add_months(sysdate,78) from dual; 加上n个月后的日期 |
|
last_day |
select last_day(sysdate) from dual; 本月的最后一天 |
|
next_day(sysdate,'星期四') |
SQL> select next_day(sysdate,'星期四') from dual; NEXT_DAY(SYSDA -------------- 18-9月 -14 |
|
日期的四舍五入 |
select round(sysdate,'month'), round(sysdate,'year') from dual; |
|
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss"今天是"day') from dual; TO_CHAR(SYSDATE,'YYYY-MM-DDHH24:MI ---------------------------------- 2014-09-17 14:40:48今天是星期三 |
||
查询员工的薪水: 两位小数 千位符,货币代码 select to_char(sal,'L9,999.99') from emp; |
||
当a=null时,返回c;否则返回b |
SQL> select nullif('abc','abc') 值 from dual; 值 --- SQL> select nullif('abc','abcd') 值 from dual; 值 --- abc |
|
coalesce 返回第一个为非空的值 |
select comm,sal,coalesce(comm,sal) 值 from emp; |
|
nvl2(a,b,c) |
当a为空时,返回c,否则返回b |
|
Case |
SQL> --给员工涨工资,总裁1000 经理800 其他400 SQL> select ename,job,sal 涨前, 2 case job when 'PRESIDENT' then sal+1000 3 when 'MANAGER' then sal+800 4 else sal+400 5 end 涨后 6 from emp; 第二种方式 SQL>select ename,job,sal 涨前, 2 decode(job,'PRESIDENT',sal+1000, 3 'MANAGER',sal+800, 4 sal+400) 涨后 5 from emp; |
|
多行函数 |
||
Sum() |
select sum(sal) from emp; |
|
Count(*) |
select count(*) from emp; |
|
平均工资 |
select sum(sal)/count(*) 一, avg(sal) 二 from emp; -null值 4: 组函数自动滤空; -null值 5: 组函数自动滤空; 可以嵌套滤空函数来屏蔽滤空功能 |
|
Having 分组后的过滤 |
select deptno,avg(sal) 2 from emp 3 group by deptno 4 having avg(sal)>2000; select deptno,avg(sal) 2 from emp 3 group by deptno 4 having avg(sal)>2000; |
|
group by的增强 |
SQL> /* SQL> group by的增强 SQL> select deptno,job,sum(sal) from emp group by deptno,job SQL> + SQL> select deptno,sum(sal) from emp group by deptno SQL> + SQL> select sum(sal) from emp SQL> SQL> == SQL> SQL> select deptno,job,sum(sal) from emp group by rollup(deptno,job) SQL> SQL> group by rollup(a,b) SQL> = SQL> group by a,b SQL> + SQL> group by a SQL> + SQL> group by null SQL> */ SQL> select deptno,job,sum(sal) from emp group by rollup(deptno,job); DEPTNO JOB SUM(SAL) ---------- --------- ---------- 10 CLERK 1300 10 MANAGER 2450 10 PRESIDENT 5000 10 8750 20 CLERK 1900 20 ANALYST 6000 20 MANAGER 2975 20 10875 30 CLERK 950 30 MANAGER 2850 30 SALESMAN 5600 DEPTNO JOB SUM(SAL) ---------- --------- ---------- 30 9400 29025 SQL> break on deptno skip 2 SQL> break on null |
|
多表查询的理解 |
||
等值连接 |
select e.empno,e.ename,e.sal,d.dname from emp e,dept d where e.deptno=d.deptno; |
|
外连接 |
||
左外连接: 右外连接: |
左外连接:当where e.deptno=d.deptno不成立的时候,等号左边的表任然被包含 SQL> 写法:where e.deptno=d.deptno(+) SQL> 右外连接:当where e.deptno=d.deptno不成立的时候,等号右边的表任然被包含 SQL> 写法:where e.deptno(+)=d.deptno |
|
自连接 |
自连接: 通过表的别名,将同一张表视为多张表 自连接:不是适合操作大表 SQL> --层次查询 SQL> select level,empno,ename,sal,mgr 2 from emp 3 connect by prior empno=mgr 4 start with mgr is null 5 order by 1; LEVEL EMPNO ENAME SAL MGR ---------- ---------- ---------- ---------- ---------- -------------------------- 1 7839 KING 5000 2 7566 JONES 2975 7839 2 7698 BLAKE 2850 7839 2 7782 CLARK 2450 7839 3 7902 FORD 3000 7566 3 7521 WARD 1250 7698 3 7900 JAMES 950 7698 3 7934 MILLER 1300 7782 3 7499 ALLEN 1600 7698 3 7788 SCOTT 3000 7566 3 7654 MARTIN 1250 7698 LEVEL EMPNO ENAME SAL MGR ---------- ---------- ---------- ---------- ---------- -------------------------- 3 7844 TURNER 1500 7698 4 7876 ADAMS 1100 7788 4 7369 SMITH 800 7902 |
oracle基本查询语句总结的更多相关文章
- Oracle分页查询语句的写法(转)
Oracle分页查询语句的写法(转) 分页查询是我们在使用数据库系统时经常要使用到的,下文对Oracle数据库系统中的分页查询语句作了详细的介绍,供您参考. Oracle分页查询语句使我们最常用的 ...
- 各种oracle参数查询语句
各种oracle参数查询语句 1.show parameter:--显示各个系统参数配置 2.select * from v$parameter;--显示各个系统参数配置 2.show paramet ...
- Oracle分页查询语句的写法
分页查询是我们在使用数据库系统时经常要使用到的,下文对Oracle数据库系统中的分页查询语句作了详细的介绍,供您参考. AD:2013云计算架构师峰会精彩课程曝光 Oracle分页查询语句使我们最常用 ...
- ORACLE中查询语句的执行顺及where部分条件执行顺序测试
Oracle中的一些查询语句及其执行顺序 原文地址:https://www.cnblogs.com/likeju/p/5039115.html 查询条件: 1)LIKE:模糊查询,需要借助两个通配符, ...
- ORACLE的查询语句
oracle的select查询语句(DQL): 语法: select //查询动作关键字 [distinct|all] //描述列表字段中的数据是否去除记录 select_list //需要查询的字段 ...
- 45 个非常有用的 Oracle 日期查询语句
日期/时间 相关查询 获取当前月份的第一天 运行这个命令能快速返回当前月份的第一天.你可以用任何的日期值替换 “SYSDATE”来指定查询的日期. SELECT TRUNC (SYSDATE, 'MO ...
- oracle 常用查询语句
一.一般日常用的脚本 1.检查源库每个节点至少3组redoselect group#,thread#,bytes/1024/1024,members,status from v$log; select ...
- oracle数据库查询语句case的用法
实现功能: 1.先查询status=2的记录,如果查询到记录则返回第一条记录的Product_Name:2.如果查询不到status=2的记录,则判断status=1的记录是否存在,不存在则返回“请耐 ...
- Oracle ->> 层级查询语句(hierarchical query)connect by
Oracle中的Connect By... Start With语句实现了递归查询或者树状查询. Connect By Prior 一方为起始(root)的ID 参考: http://www.360d ...
- Oracle数据库查询语句
编写以下查询的SQL语句,以scott用户的emp表和dept表作为查询数据: 1.列出至少有一个员工的所有部门. SQL语句: select * from SCOTT.DEPT where dept ...
随机推荐
- IntelliJ IDEA使用(一)基本设置与类、方法模板设置
其实之前一直开发都是在使用的是Eclipse,但是最近在做Maven项目的时候要用IntelliJ IDEA,据说这个idea功能非常的强大,最近在使用的时候发现如果适应的真的是非常的强大.感觉是比E ...
- 微信小程序开发历程
小程序: 帮助文档:(https://mp.weixin.qq.com/debug/wxadoc/dev/index.html) 优势: 一种无须安装即可运行的程序,与订阅号,服务号是同一 ...
- Core Java 谈谈HashMap
说起Java的HashMap相信大家都不是很陌生,但是对于HashMap内部结构有些同学可能不太了解,咱们下一步就将其展开. HashMap是基于Hash算法的,同理的还有HashSet和HashTa ...
- NHibernate教程(18)--对象状态
本节内容 引入 对象状态 对象状态转换 结语 引入 在程序运行过程中使用对象的方式对数据库进行操作,这必然会产生一系列的持久化类的实例对象.这些对象可能是刚刚创建并准备存储的,也可能是从数据库中查询的 ...
- ServletRequest的一些知识点
浏览器向服务器的请求(浏览器将数据发送给服务器时,数据存放的地方) 请求方式:GET和POST * GET:发送的数据,追加在请求的URL之上 * POST:发送的数据在HTTP请求体中 浏览器发送数 ...
- 九度OJ 1013 开门人和关门人
#include <iostream> #include <string.h> #include <sstream> #include <math.h> ...
- RobotFramework自动化测试框架-移动手机自动化测试Input Text和Click Button关键字的使用
Input Text和Click Button Input Text 关键字一般用来给输入框进行输入操作,该关键字接收两个参数[ locator | text ]. 示例1:启动安卓手机上一个APP的 ...
- 201521123082《Java程序设计》第4周学习总结
201521123082<Java程序设计>第4周总结 标签(空格分隔): java 1. 本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内 ...
- 201521123077 《Java程序设计》第8周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 泛型类 利用泛型可以写出一个更加通用的类,比如下面的例子: class simpleHolder<T&g ...
- 201521123064 《Java程序设计》第8周学习总结
1. 本章学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 集合内容见:<Java程序设计>第7周学习总结 1.2 选做:收集你认为有用的代码片段 ① Jav ...