03【玩轉Oracle】初識查詢語句及其條件使用

這章節講的主要是查詢(select)語句的使用

不同終端的Oracle連接(已更)
table.sql腳本的執行(已更)

1.字段的比較
select * from 表名 where 表中的字段名 < 80 ;
select * from SELECTIVEINFO where GRADE <80 and rownum <= 5;

2.限制查詢行數
select * from 表名 where rownum < 行數; (這裏使用 = 會提示’不可行’,只能<=)
select * from SELECTIVEINFO where rownum < 5;

3.比較日期
select * from 表 where 日期字段名 > ‘yyyy-mm-dd’;
select * from SELECTIVEINFO where SELECTIVEDATE > '04-12月-10';
(後面的字符串格式要與日期字段的格式要一致,否則提示’無效xxx’)
如果不一致仍要比較,可以使用代碼改格式,如下:(有錯誤,查詢出來顯示了全部行)
如select * from SELECTIVEINFO where to_char(SELECTIVEDATE,‘yy-mm-dd’) > ‘04-12-19’;

4.查詢字段不等於’xx’的
select * from 表名 where 字段名 <> ‘xx’ ;
select * from 表名 where 字段名 not in ‘xx’ ;
select * from 表名 where 字段名 not like ‘xx’ ;
select * from SELECTIVEINFO where classid <> 1;
select * from STUDENTINFO where STUDENTNAME not in 'a';
select * from STUDENTINFO where STUDENTNAME not like 'a';

5.between… and…範圍
select * from SELECTIVEINFO表名 where 字段名 between 下限值 and 上限值;
select * from SELECTIVEINFO where grade BETWEEN 60 and 75;

6.IN運算符
select * from 表名 where 字段名 in(字段值1,字段值2,字段值…);
select * from SELECTIVEINFO where classid in(1,3);
解釋:在表SELECTIVEINFO中查詢字段classid爲1或者3的行。

作業:

東軟公司覃老師提供
7.Like 運算符(模糊查詢)
% 代表零或任意更多的字符
_ 代表一個字符

①查詢姓名爲h開頭的學生
select * from STUDENTINFO where studentname like 'h%';

②查詢學生名字只有一個字符的學生
select * from STUDENTINFO where studentname like '_';

③查詢學生名字第二個字符爲e的學生
select * from STUDENTINFO where studentname like '_e%';

④查詢地址以”no_”開頭的學生信息
select * from STUDENTINFO where address like 'no@_%' escape '@';

⑤查詢fax爲null的學生id和fax
select studentid,fax from STUDENTINFO where fax is null;

⑥查詢倒數第二個字符爲n的學生信息
select * from STUDENTINFO where studentname like '%n_';

作業:

東軟公司覃老師提供
8. AND運算符
select * from表名 where 字段名 > 值 and 字段名 in(值1,值2);
select * from SELECTIVEINFO where grade > 85 and classid in(1,3);

9.OR運算符
select * from 表名where 字段名> 值 or 字段名 = 值 ;
select * from SELECTIVEINFO where grade > 85 or classid = 1;

10.NOT運算符
select * from 表名 where 字段名 is not 值;
select * from SELECTIVEINFO where classid is not 1;

11.邏輯運算符:也就是多重條件
反正就記住多加括號即可,不要自以爲結果是自己想的那亞子 。

作業:

東軟公司覃老師提供
12.ORDER BY語句
select * from SELECTIVEINFO where grade < 85 order by GRADE desc;
ASC升序(默認)
DESC降序(總是記不住這兩個單詞,就記住兩個單詞後面都是SC就行了 )
①ORDER BY 子句必須寫在SELECT語句的最後
②空值null在升序排列中排在最後,在降序排列中排在最開始;
③ORDER BY 可以排序沒有select中的字段
select NAME from SELECTIVEINFO where grade < 85 order by GRADE;
④可以多列排序
select NAME from SELECTIVEINFO where grade < 85 order by GRADE ,NUM_ID;
⑤可以以select字段的序號來排序
select NAME,GRADE ,NUM_ID from SELECTIVEINFO where grade < 85 order by 1 ,3;這裏的13對應的是GRADENUM_ID

作業:

東軟公司覃老師提供

1.select 員工姓名,部門編號 from 表 where 部門編號 in(20,30) order by 工資 ASC;
2.select 員工姓名,部門,編號,工資 from 表 where (工資 between 2000 and 3000) and 部門 not in(10) order by 部門 ,工資DESC。
3.select 員工姓名,入職日期,職位 from 表 where to_char(入職日期,'yy') = '82' and 職位 like in(SALES%,MAN%) order by 入職日期 DESC;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章