SQL文法協助

來源:互聯網
上載者:User
文章目錄
  • 數字類型查詢
  • 字串類型查詢
  • 日期類型查詢
  • 多條件查詢

假設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. 下例通過notlike組合使用,查詢日期為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文法均要使用英文半形!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.