Oracle 查詢操作

2021-10-10 11:43:40 字數 4542 閱讀 4036

1.select * from emp;

select empno,ename,job,mgr,hiredate,sal,comm,deptno from emp;

2.別名用法

select empno as 員工編號 , ename as 員工姓名 from emp;

3.除重複的資料

select distinct job from emp;

4.字串 連線查詢

select '編號是: 』 || empno || 『的雇員,姓名是:』 || ename || 『工作是:』 || job from emp;

5.查詢中四則運算

查詢每個雇員的年薪

select ename ,sal *12 「年薪」 from emp;

select ename ,sal *12 income from emp;

查詢工資大於1500的所有雇員

select * from emp where sal > 1500;

非空和空的限制

查詢每月能得到獎金的雇員

select * from emp where comm is not null;

select * from emp where comm is null;

查詢工資大於1500並且有獎金領取的雇員 and

select * from emp where comm is not null and sal > 1500;

查詢工資大於1500並且有獎金領取的雇員 or

select * from emp where comm is not null or sal > 1600;

查詢工資不大於1500和沒有獎金的人 語法:not(查詢條件)

select * from emp where not (comm is not null and sal >1500);

select * from emp where sal >= 1500 and sal <= 3000;

between and 不僅可以使用在數值之間,也可以用在日期的區間

select * from emp where sal between 1500 and 3000;

select * from emp where hiredate between to_date(『1981/1/1』, 『yyyy/mm/dd』) and to_date(『1981/12/31』, 『yyyy/mm/dd』);

oracle中的查詢條件中查詢條件的值是區分大小寫的

select * from emp where ename =『smith』;

select * from emp where ename =『smith』;

如果使用之前的做法可以使用or關鍵字

查詢雇員編號是7369,7499,7521的雇員編號的具體資訊

select * from emp where empno = 7369 or empno =7499 or empno = 7521

實際上,此時指定了查詢範圍,那麼sql可以使用in關鍵字

select * from emp where ename in('smith ',『allen』,『ward』)

在常用的站點中經常會有模糊查詢,即:輸入乙個關鍵字,把符合的內容全部的查詢出來,在sql中使用like語句完成。

在like中主要使用以下兩種萬用字元

「%」:可以匹配任意長度的內容

「_」:可以匹配乙個長度的內容

在like中如果沒有關鍵字表示查詢全部

select * from emp where ename like』%%』

查詢名字中帶有「m」的雇員

select * from emp where ename like 『%m%』

查詢雇員編號不是7369的雇員資訊

select * from emp where empno <> 7369;

orselect * from emp where empno != 7369;

查詢雇員的工資從低到高

select * from emp order by sal;

select * from emp order by sal desc;

order by 列名 預設的排序規則是公升序排列,可以不指定asc,如果按著降序排列必須指定desc

如果存在多個排序字段可以用逗號分隔

select * from emp order by sal asc,hiredate desc;

a : 字元函式

把小寫的字元轉換成大小的字元

select upper(『smith』) from dual;

把大寫字元變成小寫字元

select lower(『smith』) from dual;

把首字元大寫

select initcap(『smith』) from dual;

字串的連線可以使用concat可以使用「||」建議使用「||」

select concat( 『hello』,『world』) from dual;

字串的擷取,使用substr,第乙個引數是源字串,第二個引數是開始索引,第三個引數結束的索引,開始的索引使用1和0效果相同

select substr( 『hello』, 1, 3) from dual;

獲取字串的長度

select length( 『hello』) from dual;

字串替換,第乙個引數是源字串,第二個引數被替換的字串,第三個是替換字串

select replace(『hello』, 『l』, 『x』) from dual;

b : 數值函式

1)四捨五入函式:round()

預設情況下round四捨五入取整,可以自己指定保留的位數。

select round(12.534) from dual;

select round (12.534,2) from dual;

2)取整:trunc(),預設全部去掉小數,也可以指定保留的位數

select trunc(12.536) from dual;

3)取餘數mod()

select mod(10, 3) from dual;

c.日期函式

日期 – 數字 = 日期

日期 + 數字 = 日期

日期 – 日期 = 數字(天)

查詢雇員的進入公司的週數。

分析:查詢雇員進入公司的天數(sysdate – 入職日期)/7就是週數

select ename ,round((sysdate - hiredate) / 7) from emp;

獲得兩個時間段中的月數:months_between()

select ename ,round(months_between(sysdate, hiredate)) from emp;

獲得幾個月後的日期:add_month()

select add_months(sysdate, 3) from dual;

4.求出下乙個星期一是什麼日期

select next_day(sysdate, 『星期一』) from dual;

求出乙個日期的最後一天

select last_day(sysdate) from dual;

d 轉換函式

to_char:字串轉換函式

查詢所有的雇員將將年月日分開,此時可以使用to_char函式來拆分

拆分時需要使用萬用字元

select empno, ename ,

to_char(hiredate,『yyyy』) 年,

to_char(hiredate,『mm』)月,

to_char(hiredate,『dd』)日

from emp;

orselect empno ,ename ,to_char(hiredate,『yyyy-mm-dd』) from emp;

在結果中10以下的月前面被被補了前導零,可以使用fm去掉前導零

select empno ,ename ,to_char(hiredate,『fmyyyy-mm-dd』) from emp;

to_char還可以給數字做格式化

把雇員的工資按三位用「,」分隔,在oracle中「9」代表一位數字

select ename ,to_char(sal ,『99,999』) from emp;

如果在錢的前面加上國家的符號可以使用「$」代表是美元,如果要使用本地的錢的單位使用「l」

select ename ,to_char(sal,』$99,999』) from emp;

select ename ,to_char(sal,『l99,999』) from emp;

to_number:數值轉換函式

to_number可以把字串轉換成數值

select to_number(『10』)+ to_number(『10』) from dual;

4)to_date:日期轉換函式

to_date可以把字串的資料轉換成日期型別

select to_date(『1985-04-22』, 『yyyy-mm-dd』) from dual;

ORACLE操作 查詢表

獲取表字段 select from user tab columns where table name 使用者表 order by column name 獲取表注釋 select from user tab comments where table name 使用者表 order by table...

Oracle查詢歷史操作記錄

1.以sysdba身份進入 2.show parameter audit 3.alter system set audit sys operations true scope spfile 4.alter system set audit trail db,extended scope spfile...

Oracle 查詢庫表操作

查詢所有表名 select t.table name from user tables t 查詢所有欄位名 select t.column name from user col comments t 查詢指定表的所有欄位名 select t.column name from user col com...