SQL语句 oracle限制和排序

2.1 where

作用:限制行的查询

例2.1.1

SQL> select dname from scott.dept 
     where deptno = 10;

2.2 Between
Between
100 and 103 :包含100和103
例2.2.1

SQL>select DNAME from scott.dept 
    where deptno between 10 and 30;

例2.3.1

SQL>select * from scott.dept;

          DEPTNO DNAME        LOC
      10 ACCOUNTING   NEW YORK
      20 RESEARCH        DALLAS
      30 SALES         CHICAGO
      40 OPERATIONS    BOSTON
      41 A%%%%%%%    A
      42 A%_%%%%%    B

 








 SQL>select dname from scott.dept
     where deptno in (40,42);

 

DNAME

--------------

OPERATIONS

A%_%%%%%

2.4 like

2.4.1 作用:模糊查找,搜索条件可以包含文字字符或数字

1)%表示零或多个字符

2)_表示一个字符

例2.4.1

SQL>select dname from scott.dept
where deptno like '1%'; 

2.4.2 单引号转义符

Escapez:转义%与_匹配字符本来的含义

方法一:SQL> select ename||q'['s sal is ]'||sal info from scott.emp;

方法二:

SQL> select ename||'''s sal is '||sal from
scott.emp;

例2.4.2

SQL> select ename||'''s sal is '||sal from scott.emp;

 

ENAME||'''SSALIS'||SAL

------------------------------------------------------------

SMITH's sal is 800

ALLEN's sal is 1600

WARD's sal is 1250

JONES's sal is 2975

MARTIN's sal is 1250

BLAKE's sal is 2850

CLARK's sal is 2450

SCOTT's sal is 3000

 

SQL> select * from scott.dept;

   DEPTNO DNAME        LOC

  10 ACCOUNTING   NEW YORK

  20 RESEARCH        DALLAS

  30 SALES         CHICAGO

  40 OPERATIONS    BOSTON

例2.4.3

SQL> insert into
scott.dept values (41,'A%%%%%%%','A');

1 row
created.

SQL> select * from scott.dept;



   DEPTNO DNAME        LOC


  10 ACCOUNTING   NEW YORK

  20 RESEARCH        DALLAS

  30 SALES         CHICAGO

  40 OPERATIONS    BOSTON

  41 A%%%%%%%    A

例2.4.4

SQL> insert into
scott.dept values (42,'A%_%%%%%','B');

1 row
created.

SQL>select * from scott.dept;



  DEPTNO DNAME        LOC


  10 ACCOUNTING   NEW YORK

  20 RESEARCH        DALLAS

  30 SALES         CHICAGO

  40 OPERATIONS    BOSTON

  41 A%%%%%%%    A

  42 A%_%%%%%    B

6 rows
selected.

2.4.3 ~

转义符:~的下一个字符如果是%,这个百分号是真实的百分号,没有特殊的含义

例 2.4.5查找A%_开头的

SQL>select * from scott.dept where dname like 'A~%~_%' escape '~';
 DEPTNO DNAME        LOC
  
 42 A%_%%%%%    B

2.5 OR

例2.5.1

SQL>select loc  from scott.dept
    where deptno >= 10
    or dname like 'A%';

2.6 NOT IN

例2.6.1

SQL>select dname from scott.dept
where deptno not in (10,40);

 

DNAME

--------------

RESEARCH

SALES

A%%%%%%%

A%_%%%%%

2.7 || 连接运算符

    SQL> select * from scott.dept;

 

    DEPTNO DNAME        LOC

----------
-------------- -------------

      10 ACCOUNTING   NEW YORK

      20 RESEARCH        DALLAS

      30 SALES         CHICAGO

      40 OPERATIONS    BOSTON

      41 A%%%%%%%    A

      42 A%_%%%%%    B

 

    
SQL>select

 dname ||' is '||deptno
          as "name" from scott.dept;
    
     
    
    name
    
    ----------------------------------------------------------
    
    ACCOUNTING is 10
    
    RESEARCH is 20
    
    SALES is 30
    
    OPERATIONS is 40
    
    A%%%%%%% is 41
    
    A%_%%%%% is 42

2.8 DISTINCT去除重复的行

用法:select distinct 列名 from 表名

例2.8.1

SQL>select id from student1;

 

      ID

----------

       1

       2

       3

       4

       5

       6

       7

       8

       9

       9

 

10 rows
selected.

 

SQL>select distinct id from student1;

 

      ID

----------

       1

       6

       2

       4

       5

       8

       3

       7

       9

 

9 rows
selected.

2.9 在sqlplus 环境里执行操作系统命令

用法:

!clear

host 命令

例2.9.1

SQL>!clear

SQL>host echo hehe

hehe

例2.9.2

SQL>host pwd

/home/oracle/Desktop

例2.9.3

在这里插入图片描述

2.10 and

例2.10.1
在这里插入图片描述

2.11 or

在这里插入图片描述

2.12 规则优先在这里插入图片描述

2.13 Order by排序

特点:放在select 语句末尾,默认升序(asc),降序是desc

2.13.1 降序
在这里插入图片描述

2.13.2用列别名排序

在这里插入图片描述

2.13.3 用列所在数字位排序

在这里插入图片描述

2.13.4 多列排序
在这里插入图片描述

2.14 替代变量

特点:临时变量用&,调用替代变量用&&,变量中的字符和日期要用单引号引起来。

用法:select 列名 from
表名 where 列名 = &新列名;

用途:

     where 子句

 
     order 子句

 
     列表达式

  
     表名


     整个语句

例2.14.1

SQL>select id,name
 2  fromstudent
3 where id = &stid
4  ;

Enter
value for stid: 2

old   3: where id = &stid

new   3: where id = 2

 

      ID NAME

----------
--------------------

       2
xixi

2.15 define创建和分配一个值到一个变量

undefine取消已定义的变量

例2.15.1

在这里插入图片描述

2.16 set verify on|off 变量替换的过程新旧值是否显示

例2.16.1

在这里插入图片描述

2.17创建用户

命令格式:create user c##创建的用户名 identified by oracle

SQL>create user c##xigua identified by oracle;

User
created.

2.18 IS(NOT) NULL空值判断

用法:select 条件 f rom 表名 where 列名 is(is
not) null;

例2.18.1

SQL>select * from student

    2 where name is not null;

 

      ID  NAME            SEX                   AGE COURSE             SCORE
      1   LISA            girl                  21  math               80

      2   xixi            girl                  22 english             90

      3   Kris Wu         boy                   20 langauage           88

      4   jen             girl                  25 history             70

      5   hz              boy                   22 computer            60

      6   xfy             girl                  21 computer            99

      7   yy              boy                   22 math                54

      8   lanxi           girl                  21 english             60

      9   hony            boy                   22 history             56

 

9 rows
selected.

猜你喜欢

转载自blog.csdn.net/ningjiu9/article/details/84137415