SQL語句2 限制和
阿新 • • 發佈:2018-12-21
2.1 where
作用:限制行的查詢
例2.1.1
SQL> select dname from
scott.dept
2
where deptno = 10;
DNAME
--------------
ACCOUNTING
2.2 Between
Between 100 and 103 :包含100和103
例2.2.1
SQL> select DNAME from scott.dept 2 where deptno between 10 and 30; DNAME -------------- ACCOUNTING RESEARCH SALES
2.3 IN
例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 2 where deptno in (40,42); DNAME -------------- OPERATIONS A%_%%%%%
2.4 like
2.4.1 作用:模糊查詢,搜尋條件可以包含文字字元或數字
1)%表示零或多個字元
2)_表示一個字元
例2.4.1
SQL>
select dname
2 from
scott.dept
3
where deptno like '1%';
DNAME
ACCOUNTING
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