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基本查询语句总结的更多相关文章

  1. Oracle分页查询语句的写法(转)

    Oracle分页查询语句的写法(转)   分页查询是我们在使用数据库系统时经常要使用到的,下文对Oracle数据库系统中的分页查询语句作了详细的介绍,供您参考. Oracle分页查询语句使我们最常用的 ...

  2. 各种oracle参数查询语句

    各种oracle参数查询语句 1.show parameter:--显示各个系统参数配置 2.select * from v$parameter;--显示各个系统参数配置 2.show paramet ...

  3. Oracle分页查询语句的写法

    分页查询是我们在使用数据库系统时经常要使用到的,下文对Oracle数据库系统中的分页查询语句作了详细的介绍,供您参考. AD:2013云计算架构师峰会精彩课程曝光 Oracle分页查询语句使我们最常用 ...

  4. ORACLE中查询语句的执行顺及where部分条件执行顺序测试

    Oracle中的一些查询语句及其执行顺序 原文地址:https://www.cnblogs.com/likeju/p/5039115.html 查询条件: 1)LIKE:模糊查询,需要借助两个通配符, ...

  5. ORACLE的查询语句

    oracle的select查询语句(DQL): 语法: select //查询动作关键字 [distinct|all] //描述列表字段中的数据是否去除记录 select_list //需要查询的字段 ...

  6. 45 个非常有用的 Oracle 日期查询语句

    日期/时间 相关查询 获取当前月份的第一天 运行这个命令能快速返回当前月份的第一天.你可以用任何的日期值替换 “SYSDATE”来指定查询的日期. SELECT TRUNC (SYSDATE, 'MO ...

  7. oracle 常用查询语句

    一.一般日常用的脚本 1.检查源库每个节点至少3组redoselect group#,thread#,bytes/1024/1024,members,status from v$log; select ...

  8. oracle数据库查询语句case的用法

    实现功能: 1.先查询status=2的记录,如果查询到记录则返回第一条记录的Product_Name:2.如果查询不到status=2的记录,则判断status=1的记录是否存在,不存在则返回“请耐 ...

  9. Oracle ->> 层级查询语句(hierarchical query)connect by

    Oracle中的Connect By... Start With语句实现了递归查询或者树状查询. Connect By Prior 一方为起始(root)的ID 参考: http://www.360d ...

  10. Oracle数据库查询语句

    编写以下查询的SQL语句,以scott用户的emp表和dept表作为查询数据: 1.列出至少有一个员工的所有部门. SQL语句: select * from SCOTT.DEPT where dept ...

随机推荐

  1. WebForm页面间传值方法(转)

    Asp.NET WEB FORMS 给开发者提供了极好的事件驱动开发模式.Asp .NET为我们提供了三种方式,一种是可以通过用QueryString来传送相应的值,再一种是通过session变量来传 ...

  2. poj 1679 Prim判断次短路

    题意:判断最短路是否唯一. 思路:先prrim一次求出最短路同时记录最短路加入的边: 然后枚举所求边,将其删除再求n-1次prim,判断再次所求得的最短路与第一次求得的次短路的关系. 代码: #inc ...

  3. JSONP(Json with padding)

    JSONP:一种非官方跨域数据交互协议 JSONP怎么产生的 JSONP的原理 看上面的来源加以理解 上面说过了,script是不受跨域影响的 那么我们可以在我们代码中引用B服务器的文件 <sc ...

  4. 福州大学软工 1715 | K 班 - 启航

    福州大学软工 1715 | K 班 - 启航 愉快的暑假已经接近尾声了,我猜很多同学的暑假都过得轻松,毕竟是夏天(空调/WiFi/西瓜).不过呢,暑假期间的老师.助教们可没有闲着,都在为接下来的软工实 ...

  5. 201521123044 《Java程序设计》第8周学习总结

    1. 本章学习总结 2. 书面作业 本题作业题集集合 1.List中指定元素的删除(题目4-1) 1.1 实验总结(见注释) public static List<String> conv ...

  6. 201521123016 《Java程序设计》第2周学习总结

    1. 本周学习总结 JAVA中string对象创建后不可修改. 使用StringBuilder编写代码,减少内存空间的占用. 字符串使用"+"拼接,拼接后其他类型会被转化为字符串. ...

  7. ★★★★[转载]Python学习笔记一:数据类型转换★★★★

    一.int函数能够     (1)把符合数学格式的数字型字符串转换成整数     (2)把浮点数转换成整数,但是只是简单的取整,而非四舍五入. 举例: 1 aa = int("124&quo ...

  8. 201521123040《Java程序设计》第13周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 书面作业 1. 网络基础 1.1 比较ping www.baidu.com与ping cec.jmu ...

  9. javascript面向对象编程笔记

    对象:一切事物皆是对象.对象是一个整体,对外提供一些操作.比如说一个收音机是一个对象,我们不需要知道它的内部结构是什么,只需要会使用外部的按钮就可以使用收音机. 面向对象:面向对象语言的标志是他们都有 ...

  10. shell脚本之变量与状态码

    目录: 前言 如何创建一个脚本 脚本调试 变量相关 变量的命令规则 bash中变量的种类 本地变量 环境变量 只读和位置变量 位置变量 查询变量 进程的退出状态与状态码 前言 在linux管理中,sh ...