Oracle 子查詢等常見的sql語句
阿新 • • 發佈:2019-02-01
oracle 對於漢字,如果資料庫字元編碼為 GBK 佔用2個位元組,如果是UTF-8則佔用3個位元組。
1.建表
2.新增欄位
3.修改欄位型別
4.刪除一個欄位
5.修改表名
5.5 修改欄位名
6.新增資料
7.插入空值
8.插入日期欄位 使用to_date
9.設定欄位為空
10.oracle中的預設日期格式是 dd-MON-yy
11.修改回話中的日期的預設格式
12.採用 delete 刪除資料庫後,可以採用rollback;回滾,要立刻回滾中間不能有操作,否則要用回滾點。
13.delete 刪除資料 表結構在,需要寫日誌,可以恢復,數度慢。
14.truncate 刪除資料 表結構還在,不需要寫日誌,不可以恢復記錄,速度快
15.建立儲存點
16.回滾到儲存點(操作失誤)
17.開啟/關閉計時器(檢視執行sql語句執行的時間)
18.一次性插入多行(將一個表中的資料複製給自己)
19.select * 和 select column 對於大量資料,會有明顯效果。
20.取消重複行 使用 distinct (可以使用 group by 替換)
21.oracle 區分字串中的大小寫。
22.在oralce 中使用 算術運算子 +-*/
23.給欄位起別名 採用 “”、不是 ‘’
24 nvl(避免null參與運算,如果null 參與運整個表示式的值為null)
25.查詢日期欄位在oralce中查詢時間預設使用 dd-MON-yy
26.and 查詢的時候新增多個條件
27.like 模糊匹配
28.查詢首字母“S“的員工
29.查詢第三個字元為 I 的員工
30.採用 in 比 or 的效率高,in 是批量處理
31.查詢 工資高於 500 或者 崗位為 MANAGEER 的僱員,同時還要姓名首寫字母大寫的J
32.升序 asc 降序 desc
33.採用別名排序(使用 列的別名 排序 查詢每個人的年工資並降序 重點 注意 有“” 和沒有“” 的區別有的時候 採用別名排序無效)
34.使用別名的時候 漢字要用“ ” 英文不需要引號。
35.資料的分組 max,min,avg,sum(求和),count(記錄數)。注意和 別名一起使用的使用 不要用“”。
36.查詢最高工資和最低工資
37.查詢 每個部門,每個崗位上的平均工資,和最高工資
38.多個分組後的 排序 如果不是分組中的列,採用“別名”排序沒有效果,此時可以採用 ”原始列“來排序
39.group by having的使用(對分組後的列進行篩選)
40.多表連線查詢注意 "笛卡爾集" 會每個表的資料 “相乘”;
41.between and 的使用(不僅僅是兩個值之間的範圍 還可以是一張含有 兩個範圍欄位和其他多個附加欄位的表) min<=x<max
42.採用多連線查詢 時排序
43.自連線:同一個表中連線查詢。(查詢每個員工的上級)
44.單行單列子查詢 (查詢和SMITH同一部門的所有員工)
45.多行單列子查詢 (查詢 工作崗位在 10號部門的工作崗位的型別中的員工資訊)
46.單行多列子查詢 (查詢和SMITH部門和崗位相同的的人 )
47.多行多列子查詢
47.5 採用子查詢 更新資料
48.all 的使用(注意用max替換) (查詢工資比 部門30所有工資都高的人)
49.any的使用(注意用 min替換)(查詢工資比30部門任意工資高的人)
50.查詢工資大於該個部門的平均工資的員工
51.查詢工資大於每個部門平均工資的員工
52.在from 子句中使用子查詢,這個子查詢會作為一個檢視來對待,因此也叫內嵌檢視,在使用的時候必須給別名 (上面的ee" 就是)
53.給列取別名的時候 可以選用 as 給表取別名的時候 不能用“as”
54.如果在查詢的時候 給列的別名 有“ ”引號,在後面的查詢中不好參與 條件的控制
55.利用結果集來建立一張新表(對於想要操作表中的資料,又怕損壞表中的資料和安全效能)
56.多個結果集之間的操作
57.採用 union ,union all, intersect,minus 比 and in or any all 的效率高
58.to_date 將 字串 轉換為 日期型別 主要用於插入 date型別欄位
59.to_char 將日期轉換為 字元型別 主要用於查詢
60.where 條件後面不能用sum,max,min,avg 函式
having 後面可以用
order by 後面可以用
61.查詢 某條記錄的相鄰兩條記錄
62 獲取表裡面的所有列名
63.獲取表中的所有列名,並按建立列的順序排序
64.修改 表中的欄位可以為 null
65.樹形 tree 結構查詢
66.轉義
67.varchar2 欄位排序
68.查詢 oralce 中的關鍵字
69:oralce update groupby count 分組統計更新
1.建表
create table student();
create table class(id number primary key,name varchar(20));
2.新增欄位
alter table student add (id numner,stuname varchar(30),sal number(5,2),birthday date);
3.修改欄位型別
alter table student modify (stuname varchar(50));
4.刪除一個欄位
alter table student drop column sal;
5.修改表名
rename student to students;
5.5 修改欄位名
alter table students rename column stuname to sname;
6.新增資料
insert into students values(1,'S001');
insert into students(id,stuname) values(2,'S002');
7.插入空值
insert into students(id,name,birthday) values(3,'S003',null);
8.插入日期欄位 使用to_date
insert into students values(4,'S004',to_date('2012-08-22 22:18:16','yyyy-mm-dd hh24:mi:ss'));
insert into students values(5,'S005',to_date('2012/08/22 22:18:16','yyyy/mm/dd hh24:mi:ss'));
9.設定欄位為空
update students set birthday=null where id=1;
10.oracle中的預設日期格式是 dd-MON-yy
11.修改回話中的日期的預設格式
alter session set nls_date_format='yyyy-mm-dd';
alter session set nls_date_format='dd-MON-yy';
12.採用 delete 刪除資料庫後,可以採用rollback;回滾,要立刻回滾中間不能有操作,否則要用回滾點。
13.delete 刪除資料 表結構在,需要寫日誌,可以恢復,數度慢。
14.truncate 刪除資料 表結構還在,不需要寫日誌,不可以恢復記錄,速度快
15.建立儲存點
save point aa: 建立後不要 使用 commit 和exit 語句,否則儲存點會釋放。
16.回滾到儲存點(操作失誤)
rollback to aa; 可以建立多個儲存點
17.開啟/關閉計時器(檢視執行sql語句執行的時間)
set timing on;
set timing off;
18.一次性插入多行(將一個表中的資料複製給自己)
insert into students(id,stuname,birthday) select * from students;
19.select * 和 select column 對於大量資料,會有明顯效果。
20.取消重複行 使用 distinct (可以使用 group by 替換)
21.oracle 區分字串中的大小寫。
22.在oralce 中使用 算術運算子 +-*/
select deptno,job,sal*13 from scott.emp where ename='SMITH';
23.給欄位起別名 採用 “”、不是 ‘’
select ename,sal*13 "年薪" from scott.emp;
24 nvl(避免null參與運算,如果null 參與運整個表示式的值為null)
//查詢 年薪和獎金 nvl(comm,0) 如果 comm 為null 就用0替換
select enmae,(sal+nvl(comm,0))*13 "年薪" from emp;
25.查詢日期欄位在oralce中查詢時間預設使用 dd-MON-yy
select ename,hiredate from scott.emp where hiredate>'1-1月-1982';
select ename,hiredate from scott.emp where hiredate=to_date('1982-1-1','yyyy-mm-dd');
26.and 查詢的時候新增多個條件
27.like 模糊匹配
like:表示0個或者多個任意字元
_:表示單個任意字元
28.查詢首字母“S“的員工
select * from emp where ename like 'S%';
29.查詢第三個字元為 I 的員工
select * from emp where ename like '__I%';//前有兩個下劃線
30.採用 in 比 or 的效率高,in 是批量處理
31.查詢 工資高於 500 或者 崗位為 MANAGEER 的僱員,同時還要姓名首寫字母大寫的J
select * from scott.emp where ename like 'J%' and (job='MANAGER' or sal>500);
32.升序 asc 降序 desc
select * from emp order by id [asc],deptno,sal desc; //多列降序用 逗號分割
33.採用別名排序(使用 列的別名 排序 查詢每個人的年工資並降序 重點 注意 有“” 和沒有“” 的區別有的時候 採用別名排序無效)
錯誤:select ename,(sal+nvl(comm,0))*13 "年薪" from scott.emp order by 年薪 desc;
正確:select ename,(sal+nvl(comm,0))*13 ysal from scott.emp order by ysal desc;
34.使用別名的時候 漢字要用“ ” 英文不需要引號。
35.資料的分組 max,min,avg,sum(求和),count(記錄數)。注意和 別名一起使用的使用 不要用“”。
36.查詢最高工資和最低工資
select max(sal),min(sal) from emp;
37.查詢 每個部門,每個崗位上的平均工資,和最高工資
select max(sal),avg(sal) from scott.emp group by deptno,job
38.多個分組後的 排序 如果不是分組中的列,採用“別名”排序沒有效果,此時可以採用 ”原始列“來排序
排序無效:select deptno,job,max(sal),avg(sal) "avgSal" from emp group by deptno,job order by 'avgSal' desc;
排序有效:
1:select deptno,job,max(sal),avg(sal) from emp group by deptno,job order by avg(sal) desc; //使用 "原始列"
2:select deptno,job,max(sal),avg(sal) ss from emp group by deptno,job order by ss desc;
39.group by having的使用(對分組後的列進行篩選)
select deptno,avg(sal) from emp group by deptno having acg(sal)>2000
40.多表連線查詢注意 "笛卡爾集" 會每個表的資料 “相乘”;
41.between and 的使用(不僅僅是兩個值之間的範圍 還可以是一張含有 兩個範圍欄位和其他多個附加欄位的表) min<=x<max
//查詢薪水在 300-2000的員工
select * from emp where sal between 300 and 2000
//根據員工的薪水查詢出員工的薪水等級
select enam,e.sal,g.grade from emp e salgrade g where e.sal between g.losal and g.hisal; //注 salgrade 是薪水等級表。
42.採用多連線查詢 時排序
select e.ename,d.dname,e.sal from emp e,dept d where e.deptno=d.deptno order by d.deptno
43.自連線:同一個表中連線查詢。(查詢每個員工的上級)
select worker.empno,worker.ename,boss.ename from emp worker,emp boss where workder.mgr=boss.empno;
44.單行單列子查詢 (查詢和SMITH同一部門的所有員工)
select * from e where deptno=(select deptno from emp where ename='SMITH');
45.多行單列子查詢 (查詢 工作崗位在 10號部門的工作崗位的型別中的員工資訊)
select * from emp where e.job in(select distinct job from emp where deptno=10);
46.單行多列子查詢 (查詢和SMITH部門和崗位相同的的人 )
select * from emp where (deptno,job)=(select deptno,job from emp where ename='SMITH');
47.多行多列子查詢
select * from emp where (deptno,job) in (select deptno,job from emm2);
47.5 採用子查詢 更新資料
1.笨方法 update emp set job=(select job from emp where enmae='SMITH'),sal=(select sal from emp wehre ename='SMITH'),comm=(select comm from emp where ename='SMITH') where ename='SCOTT';
2.好方法 update emp set (job,sal,comm)=(select job,sal,comm from emp where='SMITH') where ename=‘SCOTT’
48.all 的使用(注意用max替換) (查詢工資比 部門30所有工資都高的人)
效率低:select * from emp where sal>all(select sal from emp where
deptno=30);
效率高:select * from emp where sal>(selec max(sal) from emp where deptno=30)
49.any的使用(注意用 min替換)(查詢工資比30部門任意工資高的人)
效率低:select * from emp where sal>any(select sal from emp where deptno=30);
效率高:selet * from emp where sal>(select min(sal) from emp where deptno=30);
50.查詢工資大於該個部門的平均工資的員工
1.select * from emp e,(select deptno,avg(sal) mysal from emp group by deptno) ee where e.deptno=ee.deptno and e.sal>ee.mysal;
2.select * from emp e where e.sal>(select avg(sal) from emp ee where e.deptno=ee.deptno group by ee.deptno);
51.查詢工資大於每個部門平均工資的員工
1:select * from emp where sal>(select max(avg(sal)) from emp group by deptno);
2.select * from emp where sal>all(select avg(sal) from emp group by deptno);
52.在from 子句中使用子查詢,這個子查詢會作為一個檢視來對待,因此也叫內嵌檢視,在使用的時候必須給別名 (上面的ee" 就是)
53.給列取別名的時候 可以選用 as 給表取別名的時候 不能用“as”
54.如果在查詢的時候 給列的別名 有“ ”引號,在後面的查詢中不好參與 條件的控制
(錯誤)select * from emp e,(select deptno,avg(sal) "mySal" from emp group by deptno) ee where ee.deptno=e.deptno and e.sal> 'mySal'
(正確) select * from emp e,(select deptno,avg(sal) mySal from emp group by deptno) ee
where ee.deptno=e.deptno and e.sal> mySal;
55.利用結果集來建立一張新表(對於想要操作表中的資料,又怕損壞表中的資料和安全效能)
create table emp4 (id,ename,sal) as select empno,ename,sal from emp;
56.多個結果集之間的操作
union:取得兩個結果集的並集,去掉重複的行
union all:取得兩個結果集的並集,不會去掉重複行
intersect:取交集
minus:取差集
union :select ename,sal,job from emp where sal>2500 union select ename,sal,job from emp where job='MANAGER'
union all select ename,sal,job from emp where sal>2500 union all select ename,sal,job from emp where job='MANAGER'
intersect: select ename,sal,job from emp where sal>2500 intersect select ename,sal,job from emp where job='MANAGER'
57.採用 union ,union all, intersect,minus 比 and in or any all 的效率高
58.to_date 將 字串 轉換為 日期型別 主要用於插入 date型別欄位
insert into emp values(9996,'小紅2','MANAGER',7782,to_date('1988/11/11','yyyy/mm/dd'),79.9,55.33,10);
select * from bi_message where createtime=to_date('2012-8-22 18:17:57','yyyy-mm-dd hh24:mi:ss');
59.to_char 將日期轉換為 字元型別 主要用於查詢
select * from emp where to_char(hiredate,'yyyy')='1980';
select * from emp where to_char(hiredate,'mm')=12;
60.where 條件後面不能用sum,max,min,avg 函式
having 後面可以用
order by 後面可以用
61.查詢 某條記錄的相鄰兩條記錄
--後一條
select * from (select * from bi_baoliao b where b.starttime < (select starttime from bi_baoliao where id=3) order by b.starttime desc) where rownum=1 union all
--前一條
select * from (select * from bi_baoliao b where b.starttime > (select starttime from bi_baoliao where id=3) order by b.starttime asc) where rownum=1
62 獲取表裡面的所有列名
select WM_CONCAT(tc.COLUMN_NAME) from user_tab_columns tc where table_name='BI_BAOLIAO'
63.獲取表中的所有列名,並按建立列的順序排序
select WM_CONCAT(COLUMN_NAME) from (select COLUMN_NAME from user_tab_columns tc where table_name='BI_BAOLIAO' order by COLUMN_ID asc)
64.修改 表中的欄位可以為 null
alter table fbb_manager_fun modify description null
65.樹形 tree 結構查詢
select f.*,level from fbb_manager_fun f start with parentid is null connect by prior id=parentid ORDER SIBLINGS BY sortid;
66.轉義
select 'update bi_user_cashcard a set sendmoney='||sum(money+nsmoney- charge+gift)||' where id='||usercardid from bi_user_cashapply b where status='2' g roup by usercardid
67.varchar2 欄位排序
//前提 attributevalue 必須從字面上能夠轉換成 int 否則會出錯
select * from fbb_user u order by cast(u.attributevalue as int)
68.查詢 oralce 中的關鍵字
select * from v$reserved_words;
select "COMMENT" from bi_tb_psubject
--如果非要用關鍵字,可以加 "關鍵字列" 進行查詢 和插入 等操作
insert into bi_tb_psubject (ID,TITLE,SUBTITLE,"COMMENT",PICTURE,INNERPIC,STYPE,STARTTIME,ENDTIME,CREATETIME,AUTHORNAME,STATUS)
values (BI_SUPER_SEQUENCE.Nextval,'月亮代表我的心','月亮惹的禍','你闖禍沒','pp','admin','1',sysdate-1,sysdate,sysdate,'luob','1');
--另外,如果 採用類是 ibatis 等xml配置檔案 由於在sql中加上了 ""因此 要用
<![CDATA[ sql ]]> 進行 宣告
69:oralce update groupby count 分組統計更新
update fbb_bagitem t1 set t1.recordtime = (select count(c.id) from fbb_bagitem m,fbb_bagitem_recommend r where m.id=r.itemid and t1.id=m.id group by m.id)
update fbb_bagitem t1 set t1.fantime = (select count(c.id) from fbb_bagitem m,fbb_bagitem_comment c where m.id=c.itemid and t1.id=m.id group by m.id)