確定范圍 | between and,not between and |
確定集合 | in,not in |
字符匹配 | like,not like |
空值 | is null,is not null |
多重條件 | and,or,not |
1。確定范圍
查詢年齡在(不在)21到24之間的學生姓名,年齡。
select sn,age from s where age between 21 and 24 (select sn,age from s where age not between 21 and 24)
2.確定集合
查詢是(不是)信息系、自動化系和計算機系的學生的姓名
select sn from s where dept in ('信息系',‘自動化系’,‘計算機系’) ( select sn from s where dept not in ('信息系',‘自動化系’,‘計算機系’))
in謂詞實際上是多個or運算符的縮寫
3.字符匹配
查詢名字中第二個字為“陽”字的學生的姓名。
select sn from where sn like '_陽%' 查詢結果 :季陽,季陽春,季陽春春
換碼字符 :escape 對通配符(%,_,[],[^])進行轉義
查詢d_n課程的課程名和學分
select cno,ct from c where cn like ‘d\_n’ escape '\'
escape'\'短語表示“\“為換碼字符,這樣匹配串中緊跟在\后面的字符”_“不再具有通配符的含義,而是取其本身字符含義,即被轉義為普通的”_“字符
4.空值的查詢
查詢所有有(缺少)成績的學生的學號和相應的課程號
select sno,cno from sc where score is not null (select sno,cno from sc where score is null)
5.多重條件查詢
and、or、not可連接多個查詢條件。優先級:not>and>or。可用括號改變運算的優先順序。
查詢計算機系年齡21以下的學生姓名。
select sn from where dept='計算機系' and age<21
查詢信息系、自動化系和計算機系的學生的姓名
select sn,sex from where dept='計算機系' or dept='自動化系' or dept='信息系'
select sn,sex from where not (dept <>'計算機系' and dept<>'自動化系' and dept<>'信息系')