oracle數據庫----DML語句(查詢select(where,order by ))

1基本查詢

1.1語法格式

在這裏插入圖片描述

1.2案例

  • 查詢所有員工記錄
- select * from employee;
  • 查詢員工號 姓名 月薪
- select id, name,salary from employee;
  • 使用別名查詢員工號 姓名 月薪
- select id as 員工號, name 姓名,salary 月薪,salary*12 年薪 from employee;

在這裏插入圖片描述

  • 在使用別名中
    • as 可以省略
    • 如果別名中有空格需要使用" "引起來。

1.3特殊函數和關鍵字

1.3.1nvl()函數

- select id as 員工號, name 姓名,nvl(salary,0) 月薪,salary*12 年薪 from employee;
  • 因爲包含NULL的表達式都會爲空 ,導致整個查詢語句都爲空,所以查詢的行有一個列爲空都無法查到整個行
    • 解決方法:nvl(a,b) 若a爲NULL 則去b值

1.3.2distinct

SELECT distinct sname,salary from class;

在這裏插入圖片描述

  • 作用:去重
  • 範圍:關鍵字後面的列

1.4 注意點

1.4.1查詢系統當前日期

  • select sysdate from dual;
  • dual 是個僞表 主要是爲了滿足sql語法的規定

1.4.2C

  • 在編寫sql語句是,出現語法錯誤 則可以用C
    在這裏插入圖片描述

1.4.3保存查詢結果

-- 地址加文件名
spool D:\result;
select * from emp;
 spool off;

2where條件

2.1where後面使用比較運算符

> < = != (< >) between and
  • 查詢員工的名字爲員工信息
select * from employee where name='莊三';
結論:表中的數據區分大小寫 但是關鍵字和列名不區分大小寫
  • 查找薪水不等於1500的員工信息
select * from employee where salary!=1500;
select * from employee where salary<>1200;
- NULL會讓整個表達式爲假
select * from employee where nvl(salary,0)<>1500;
  • 查詢入職日期爲2015-06-12的員工信息
select * from employee where entry_date='2015-06-12';
- 注意兩者的時間格式一定要相等
 
  • 查詢工資介於1000-2000的員工信息
select * from employee where salary>=1000 and salary<=2000;
select * from employee where salary BETWEEN 1000 and 2000;
結論 between是閉區間包含它的邊界值

2.2邏輯運算符

or and not
  • 查詢id爲1 或 2 的員工信息
select * from employee where id=1 or id=2;
  • 查詢id爲1 或 salary=1500 的員工信息
select * from employee where id=1 or salary=1500;
  • 關於and or的優化問題
  • and時:把最容易出現假的表達式放在右邊,因爲一個爲假 mysql 就不會去判斷後面的表達式
  • or時 最容易爲出現真的表達式放在右邊,因爲一個爲真 mysql 就不會去判斷後面的表達式
  • 在where條件中 and的優先級比or高 所以若where條件中有or和and時 使用or應該拿括號括住

查詢salary爲NULL 的員工信息

select * from employee where salary is NULL;
select * from employee where salary is not NULL;
判斷一個值是否爲NULL時 不用等於號 用 is / is not

2.3集合in / not in

  • 查詢員工是1或者2的員工信息
select * from employee where id in (1,2);
select * from employee where id not in (1,2);
  • 結論
    • 可以再in 中使用NULL 因爲id in(1,2)相當於id=1 or id=2;
    • 不可以再not in 中使用NULL 因爲id not in(1,2)相當於id!=1 and id!=2;
      在這裏插入圖片描述

2.3模糊查詢like(%匹配多個字符,_匹配一個字符)

  • 查詢員工姓名字母是"莊"的員工信息
select * from employee where name like '莊%'

在這裏插入圖片描述

  • 查詢員工姓名字母2個字的長度的員工信息
select * from employee where name like '__'

在這裏插入圖片描述
注意如果要查詢名字裏面有 ‘_’ 字符的人 要使用轉義字符

select * from employee where name like '%\_%' escape '\';
escape設置轉義字符
  • 注意: 在模糊查詢中如:’%d% '只有後面的%纔會通過索引來查詢

3排序函數

select 列名 from 表名 where 表達式 order by ---
  • 排序有兩種排序 默認升序asc 降序 desc

3.1使用列名進行排序

  • 使用id進行排序
select * from employee ORDER BY id;
select * from employee ORDER BY id desc; 
  • 使用薪水進行排序
select * from employee ORDER BY salary ;
select * from employee ORDER BY salary NULL last;
NULL的值表現爲無窮大
  • id 進行升序排序 salary 進行降序排序
select * from employee ORDER BY id,salary desc;
在進行多列排序時 先按照第一列進行排序 ,然後在第一例相同的區間在進行第2列排序依次內推

3.2使用序號進行排序 序號:select後面列名的順序

select id,name,sex, salary from employee ORDER BY 4;

在這裏插入圖片描述

3.3使用別名進行排序

select id,name,sex, salary,salary*12 '年輸入' from employee ORDER BY '年輸入';

3.4使用表達式進行排序

select id,name,sex, salary,salary*12  from employee ORDER BY salary*12;

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章