case when 子查询_Oracle数据库-单表查询
本章涉及單張表中的查詢語句,包含常用的條件查詢、范圍查詢、模糊查詢等,跨表查詢后續將會介紹。
1.基本查詢語句
格式:SELECT[DISTINCT] column_name,…|*
FROM table_name
[WHERE conditions]
備注:添加DISTICNT可以在查詢時剔除重復的字段
先創建一張名為userinfo_s1的表
SQL>create table userinfo_s1
(id number(10,0) primary key,
Username varchar2(30),
Salary number(8,2));
Insert into userinfo_1 Values(1,'robert',888.00);
Insert into userinfo_1 Values(2,'jack',777.7);
Insert into userinfo_1 Values(3,'rose',555);
在SQL*PLUS中設置格式
格式:COLUMN column_name HEADING new_name
SQL>col username heading 用戶名;
格式:COLUMN column_name FORMAT dataformat
SQL>col username format a5;
備注:設置字符型的數據長度,以a起頭后面跟上要保留的長度。例如,上面這條語句使得username改成5位,也就是robert 變成rober。
SQL>col salary format 9999.9;
備注:設置數據型格式,小數點之后的9表示保留的小數位數。例如,上面這條語句使得數據888.00變成888.0。假如設置的整數部分長度比表中某數據的整數部分位數要少,會使得相應數據發生錯誤。下面這條語句,會使得888.00變成######。
SQL> col salary format 99.9;
也可以在數據格式設置中添加符號
SQL>col salary format $9999.99;
要清除設置的數據格式,可用CLEAR
SQL> COLUMN column_name clear;
2.查詢表中的所有字段及指定字段
查詢所有字段
格式:select *from table_name;
先設置格式再對表進行查詢
SQL>col id heading 編號;
SQL>col username heading 用戶名;
SQL>col salary heading 工資;
SQL>select *from users;
3.給字段設置別名
注意,這里給字段設置別名并不會直接更改原表中的字段名稱,僅僅是方便我們對數據庫進行操作。
格式:SELECT column_name AS new_name,… |AS可省略,注意留出空格
FROM table_name
SQL>select id as 編號, username as 用戶名, salary as 工資
From userinfo_s1;
4.在SELECT語句中使用運算符
表達式由操作數和運算符組成。oracle數據庫中操作數可以說變量、常量和字段。
運算符主要有三類,算術運算符(+,-,*,/),比較運算符(>,>=,<,<=,=,<>)和邏輯運算符(and or not),其中<>表示的是不等于。注意這些運算符是有優先級的,可類比C++語言中運算符優先級。
展示之前創建的表userinfo_s1
->ID USERNAME SALARY
----------- ---------------- -------------
1 robert 888.00
2 jack 777.7
3 rose 555
使用算術運算符
SQL>select id,username,salary+100 from userinfo_s1;
注意此時僅僅是在查詢結果中顯示salary每個數據加100,但是原表中的數據不改變。
使用比較運算符
SQL>select username from userinfo_s1
Where salary>600;
使用邏輯運算符
SQL>select username from userinfo_s1
Where salary>600 and salary<800;
5.帶條件的查詢
單一條件查詢
SQL>select username,salary from userinfo_s1 where id=1;
多條件查詢
如查詢userinfo_s1中姓名是jack或者工資高于600低于700的員工信息
SQL>select* from userinfo_s1
Where username='jack' or (salary>600 and salary<700);
為了使邏輯更清晰,可以添加括號。
SQL>select*from userinfo_s1
Where not(username='robert');
6.模糊查詢
與精確匹配查詢相對應,模糊查詢相當于模式匹配,查詢包含某個模式的結果。
模糊查詢主要用到兩個通配符:下劃線_和百分號%,其中一個下劃線代表一個字符而百分號可以代表任意長度字符。
例如,要從之前創建的userinfo_s1表中查詢用戶名以r開頭的用戶信息
SQL>select *from userinfo_s1
Where username like 'r%';
要查詢用戶名第二個字符是o的用戶信息
SQL>select *from userinfo_s1
Where username like '_o%';
要查詢用戶名中含有e的用戶信息
SQL>select* from userinfo_s1
Where username like '%e%';
7.范圍查詢
BETWEEN…AND語句
SQL> select* from userinf_s1
Where salary between 600 and 800;
備注:不同數據庫中對BETWEEN… AND端點值是否包含在內有不同的規定。在MySQL中是左閉右開,即包含左端點不包含右端點,而在Oracle11g中,左右端點都包含在內。
IN/NOT IN語句
假如要查詢用戶名是jack或者rose的用戶信息
SQL> select* from userinf_s1
Where username in ('jack','rose');
8.對查詢結果進行排序
格式:SELECT…FROM…[WHERE]
ORDER BY column1 DESC/ASC,…
備注:DESC表示按照字段降序排列,ASC表示升序排列。
若在userinfo_s1表中按照ID字段降序排列,查詢用戶信息
SQL>select *from userinfo_s1
Order by id desc;
可使用多個排序,按照先后原則,在前一排序中字段相等可繼續向后執行排序。為此,可向userinfo_s1表中再添加一行數據進行演示。
SQL>insert into userinfo_s1
Values(4,'rose',666);
SQL>select *from userinfo_s1
Order by username desc, salary asc;
9.Case…when語句
格式:CASE column_name
WHEN value1 THEN result1,…
[ELSE result]END
SQL>select username,
case username
when 'robert' then '前端工程師'
When 'jack' then'后端工程師’
Else '其他' end as '職位'
From userinfo_s1
->USERNAME 部門
------------ ---------
Robert 前端工程師
jack 后端工程師
rose 其他
rose 其他
格式:CASE
WHEN column_name=value1
THEN result1,…[ELSE result] END
SQL>select username,Case
when username='robert' then '前端工程師'
when username='jack' then '后端工程師'
else '其他' end as'職位'
from userinfo_s1;
后一種格式比較靈活,可以使用表達式,比如
SQL>select username,case
When salary<800 then '一般'
When salary>=800 then '高'
End as '工資水平'
From userinfo_s1;
備注:若不加ELSE,則不滿足CASE條件的數據對應為空值。
10.Decode函數
格式:decode(column_name,value1,result1,…,defaultvalue)
SQL>select username,decode(username, 'robert', '前端工程師', 'jack', '后端工程師', ‘其他') as '職位' from userinfo_s1;
總結
以上是生活随笔為你收集整理的case when 子查询_Oracle数据库-单表查询的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 做二代试管婴儿的成功率有多少
- 下一篇: python控制结构实训_《python