1. 程式人生 > >SQL語句 oracle限制和排序

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.