過濾和排序資料

2021-10-03 10:16:19 字數 4790 閱讀 5592

基本語法

select

*from

"tablename"

where

"column"

"operator"

"condition"

;

operator

比較運算

= 等於(不是==)

> 大於

>= 大於等於

< 小於

<= 小於等於

<> 不等於(也可以是!=

)關鍵字

between…and: 介於兩值之間。

in 在集合中

notin 不在集合中

like 模糊查詢

邏輯運算

and 邏輯並

or 邏輯或

not 邏輯非

字元和日期要包含在單引號中。

字元大小寫敏感,日期格式敏感。

預設的日期格式是 dd-mon-rr

例如:

sql

>

select

*from emp

*where deptno=

10; //查詢emp 表 deptno 列中 等於 10 的資料,以行的方式顯示

select

*from emp //字元要包含在單引號內, 且對大小寫敏感

where ename =

'king'; //查詢 emp 表 ename 列中為'king' 的資料,以行的方式顯示

sql>

select

*from emp //預設的日期格式是 dd-mon-rr

where hiredate=

'17-11月-81'; //查詢emp 表 hiredate 列中 時間為 81年11月17日的資料,以行的方式顯示

sql>

select

*from v$nls_parameters;

//查詢系統當前日期格式,v$nls_parameters 類似資料字典

sql>

alter

session

set nls_date_format =

'yyyy-mm-dd'

;//修改日期格式

sql>

alter

session

set nls_date_format =

'dd-mon-rr'

;//將日期改回預設格式

sql>

select

sysdate from dual //檢視系統時間

between…and

包含邊界

小值在前,大值在後。(對於日期也是如此)

//使用比較級運算子查詢範圍

sql>

select

*from emp

where sal >=

2000

and sal <

3000

;//即查詢emp表sal列中 1999 ~ 3000 之間的資料

//使用bteween 關鍵字

sql>

select

*from emp

where sal between

2000

and3000

;//即查詢emp表sal列中 1999 ~ 3001 之間的資料

sql>

select

*from emp

where hiredate between

'1-2 月-81'

and'30-1 月-82'

;//即查詢emp表hiredate 列中 81年2月至82年2月 之間的資料

in:在集合中。(not in 不在集合中)

不能在not in的集合中使用null

not in (10,20,null) == > if(deptno != 10 && deptno != 20 && deptno != null)

最後乙個 表示式永遠不會成立,所以整個表示式為假

實驗:

sql

>

select sysdate from dual

2where1=

1;//where 子句的依據是什麼?

sysdate //result

--------------02-

3月 -

20

in , not in 使用

//使用比較級運算子查詢是否在集合

sql>

select

*from emp

where deptno =

10or deptno =20;

//即查詢emp表 deptno列中等於10或20 的資料

//使用in 關鍵字

sql>

select

*from emp

where deptno in(10

,20);

//等價上一句語句

//使用比較級運算子查詢是否不在集合

sql>

select

*from emp

where deptno <>

10or deptno <>20;

//即查詢emp表 deptno列中不等於10 或20 的資料

// 使用not in 關鍵字

sql>

select

*from emp

where deptno notin(

10,20)

;//等價上一句語句

模糊查詢

『%』匹配任意多個字元, 『_』匹配乙個字元。

』 \ 』 轉義字元,使用斜槓時需要使用 escape 』』

『』 兩個雙引號轉義, 即將特殊符號 單引號 改變成可輸出的單引號

sql

>

select

*from map

where ename like

's%'

;//即查詢map表 ename 列中以s開頭的資料

sql>

select

*from map

where ename like

'____'

;//即查詢map表 ename列中任意4個字元的資料

sql>

select

*from map //即查詢map表 ename列中 包含_字元的資料

where ename like

'%\_%'

escape

'\' ;

sql> select

'hello '' world' from dual ; //使用兩個單引號來完成轉義。

order by 子句在select語句的結尾。

asc(ascend): 公升序。預設採用公升序方式。

desc(descend): 降序

基本語法:

select   last_name, job_id, department_id, hire_date

from employees

order

by hire_date ;

order by 的使用

//使用列名

sql>

select

*from map

order

by sal; //即將 map表 按sal列中的資料 進行排序顯示,排序格式預設為 asc

//使用表示式

sql>

select ename, sal, sal*

12from emp

order

by sal *

12desc

;//即將 map表 按表示式(sal * 12)進行排序,排序格式顯式指定為desc 即降序

//使用序號

sql>

select ename, sal, sal*

12from emp //序號即指定的列號,預設:ename→1, sal→2,sal*12→3

orderby2

desc ; //將將 map表 按序號進行排序,排序格式顯式指定為desc

//使用多列排序

sql>

select

*from emp

*order

by deptno, sal;

//即將map表按deptno 先進行排序, 排序的結果再按 sal 進行排序, 排序格式預設為 asc

sql>

select

*from emp

order

by deptno desc

, sal desc

;//即將map表按deptno 先進行排序, 排序的結果再按 sal 進行排序, 排序格式顯式指定為 desc

排序的規則

按照select語句中的列名排序

按照別名列名排序

按照select語句中的列名的順序值排序

如果要按照多列進行排序,則規則是先按照第一列排序,如果相同,則按照第二列排序;以此類推

ORACLE SQL過濾和排序資料

在查詢過濾行就是查詢你需要的內容和資料,並進行排序 但是我們過濾的過程中需要的語句是 where where語句是緊隨 from 子句 例如 select sno,sname,sbirthday from student where sname 曾華 紅色部分不論是數字和日期都可以過濾 但是字元和日...

第二章 過濾和排序資料總結與測試

第二章 過濾和排序資料 總結 1.where子句緊隨from子句 2.查詢last name為 king 的員工資訊 select first name,last name from employees where last name king king沒有加上單引號select first nam...

Oracle學習 限定和排序資料

字串和日期 字串和日期要用單引號擴起來 字串是大小寫敏感的 日期值是格式敏感的 預設的日期格式是 dd mon rr select last name job id department id from employees where last name whalen 比較條件 其它比較運算子 使用...