文章目錄
- 數字類型查詢
- 字串類型查詢
- 日期類型查詢
- 多條件查詢
假設student表中儲存著學生的考試成績,資料資訊如下:
| ID |
Name |
Type |
Score |
PassDate |
| 1 |
張三 |
數學 |
90 |
2008-09-01 |
| 2 |
李四 |
語文 |
93 |
2007-12-02 |
| 3 |
王五 |
化學 |
70 |
2008-01-10 |
其中,各列的資料類型如下:
ID NUMBER 數字類型
Name VARCHAR2 字串類型
Type VARCHAR2 字串類型
Score NUMBER 數字類型
PassDate DATE 日期類型
-----------------------------------------------------------------------------------------
數字類型查詢
對於數字類型列,可使用的比較符有:=、!=、>、<、>=、<=、between...and...、in、not in
例, 執行以下SQL語句進行查詢:
1. select * from student where score = 93
查詢結果:
| ID |
Name |
Type |
Score |
PassDate |
| 2 |
李四 |
語文 |
93 |
2007-12-02 |
2. select * from student where score >= 90
查詢結果:
| ID |
Name |
Type |
Score |
PassDate |
| 1 |
張三 |
數學 |
90 |
2008-09-01 |
| 2 |
李四 |
語文 |
93 |
2007-12-02 |
3. select * from student where score between 90 and 100
查詢結果:
| ID |
Name |
Type |
Score |
PassDate |
| 1 |
張三 |
數學 |
90 |
2008-09-01 |
| 2 |
李四 |
語文 |
93 |
2007-12-02 |
-----------------------------------------------------------------------------------------
字串類型查詢
對於字串類型列,在查詢時需要在字串兩邊添加單引號('),可使用的比較符有:=、!=、like、in、not like、not in
例, 執行以下SQL語句進行查詢:
1. select * from student where name = '李四'
查詢結果:
| ID |
Name |
Type |
Score |
PassDate |
| 2 |
李四 |
語文 |
93 |
2007-12-02 |
2. 當不確定字串內容時,可使用like進行查詢,其中(%)意為預留位置。
select * from student where type like '%數%'
查詢結果:
| ID |
Name |
Type |
Score |
PassDate |
| 1 |
張三 |
數學 |
90 |
2008-09-01 |
3. 當需要同時查詢多個字串時,可使用in進行查詢,其中每個字串需用(,)隔開。
select * from student where type in ('語文', '化學')
查詢結果:
| ID |
Name |
Type |
Score |
PassDate |
| 2 |
李四 |
語文 |
93 |
2007-12-02 |
| 3 |
王五 |
化學 |
70 |
2008-01-10 |
-----------------------------------------------------------------------------------------
日期類型查詢
對於日期類型列,在查詢時也需要在日期兩邊添加單引號('),並且日期格式寫法為:01-AUG-08,即2008-08-01(英文簡寫不區分大小寫)。
可使用的比較符有:=、!=、>、<、>=、<=、between ... and ...、like、in、not like、not in
例, 執行以下SQL語句進行查詢:
1. select * from student where passdate <= '1-aug-08'
查詢結果:
| ID |
Name |
Type |
Score |
PassDate |
| 2 |
李四 |
語文 |
93 |
2007-12-02 |
| 3 |
王五 |
化學 |
70 |
2008-01-10 |
2. 下例通過not與like組合使用,查詢日期為07年的資料。
select * from student where passdate not like '%08%'
查詢結果:
| ID |
Name |
Type |
Score |
PassDate |
| 2 |
李四 |
語文 |
93 |
2007-12-02 |
3. 當需要查詢某個時間段時,可使用between ... and ...進行查詢。
select * from student where passdate between '01-Jan-07' and '01-May-08'
查詢結果:
| ID |
Name |
Type |
Score |
PassDate |
| 2 |
李四 |
語文 |
93 |
2007-12-02 |
| 3 |
王五 |
化學 |
70 |
2008-01-10 |
-----------------------------------------------------------------------------------------
多條件查詢
在查詢過程中,可能會需要輸入多個查詢條件,這時可使用and、or進行多條件組合。
例, 執行以下SQL語句進行查詢:
1. 下例將查詢語文考試成績在90分以上的同學,需要使用and符。
select * from student where type = '語文' and score >= 90
查詢結果:
| ID |
Name |
Type |
Score |
PassDate |
| 2 |
李四 |
語文 |
93 |
2007-12-02 |
2. 下例將查詢考試成績在90分以上或60分以下的同學,需要使用or符。
select * from student where score > 90 or score < 60
查詢結果:
| ID |
Name |
Type |
Score |
PassDate |
| 2 |
李四 |
語文 |
93 |
2007-12-02 |
-----------------------------------------------------------------------------------------
注意:以上藍色SQL文法均要使用英文半形!