国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

復(fù)雜查詢語句的使用

2024-07-21 02:40:50
字體:
供稿:網(wǎng)友

  .查詢語句的使用
使用 select語句和子查詢(subquery)可以從一個或多個表,視圖,實體試圖中返回數(shù)據(jù).
 
1.1相關(guān)子查詢
可以將子查詢(as subquery)或in或exists當成where的一個條件的一部分,這樣的查詢稱為子查詢
  .where中可以包含一個select語句的子查詢
  .where中可以包含in,exists語句
  .最多可以嵌套16層
  .層次過多會影響性能
  [例]簡單子查詢實例
  查詢是否有的專家既以研究所的名義來申請基金項目,又以大學(xué)系為單位申請項目
  (按規(guī)定只能以一個單位來申請)
  SQL> create table univ_subject
  2    (
  3       name                 varchar2(12) not null,
  4       per_id                number     not null,
  5      dept_name       varchar2(20)            
  6    );
  SQL> insert into univ_subject  values('gaoqianjing',1001,'信息工程系');
  SQL> insert into univ_subject  values('wangbing',1002,'物理系');
  SQL> insert into univ_subject  values('liming',1003,'化學(xué)系');
  ===============
   SQL> create table  colle_subject
  2     (
  3              colle_name    varchar2(20),
  4              per_id              number
  5     );
  SQL> insert into colle_subject values('電子研究所',1001);
  SQL>  insert into colle_subject values('物理研究所',1005);
  ================
  SQL> select name,per_id,dept_name from univ_subject where per_id in
  2    (select per_id from colle_subject);   NAME            PER_ID   DEPT_NAME
  ------------          ---------     --------------------
  gaoqianjing  1001      信息工程系############################復(fù)雜查詢語句的使用#####################################
1.查詢語句的使用
使用 select語句和子查詢(subquery)可以從一個或多個表,視圖,實體試圖中返回數(shù)據(jù).
 
1.1相關(guān)子查詢
可以將子查詢(as subquery)或in或exists當成where的一個條件的一部分,這樣的查詢稱為子查詢
  .where中可以包含一個select語句的子查詢
  .where中可以包含in,exists語句
  .最多可以嵌套16層
  .層次過多會影響性能
  [例]簡單子查詢實例
  查詢是否有的專家既以研究所的名義來申請基金項目,又以大學(xué)系為單位申請項目
  (按規(guī)定只能以一個單位來申請)
  SQL> create table univ_subject
  2    (
  3       name                 varchar2(12) not null,

  4       per_id                number     not null,
  5      dept_name       varchar2(20)            
  6    );
  SQL> insert into univ_subject  values('gaoqianjing',1001,'信息工程系');
  SQL> insert into univ_subject  values('wangbing',1002,'物理系');
  SQL> insert into univ_subject  values('liming',1003,'化學(xué)系');
  ===============
   SQL> create table  colle_subject
  2     (
  3              colle_name    varchar2(20),
  4              per_id              number
  5     );
  SQL> insert into colle_subject values('電子研究所',1001);
  SQL>  insert into colle_subject values('物理研究所',1005);
  ================
  SQL> select name,per_id,dept_name from univ_subject where per_id in
  2    (select per_id from colle_subject);  NAME            PER_ID   DEPT_NAME
  ------------          ---------     --------------------
  gaoqianjing  1001      信息工程系1.2外連接
 [例]外連接實例
 招生中所有學(xué)生的信息放在students表中,而部分有特長的學(xué)生在另一個表中stuent_skill中同樣有該學(xué)生
 的信息?,F(xiàn)在要全部列出所有學(xué)生,假如某個學(xué)生在表student_skill中就有其特長信息,并顯示特長信息,假如
 某個學(xué)生沒有特長就顯示特長問空.
 SQL>  create table students
  2    (
  3       st_id    varchar2(20),
  4       name  varchar2(10),
  5       age      number(2),
  6       tol_score   number(3)
  7    ) ;
SQL>   insert into students values('973231','wangbindu',22,501);
SQL>   insert into students values('973232','zhuzhijing',21,538);
SQL>  insert into students values('973233','gaojing',21,576);
===================SQL>  create table student_skill
  2   (
  3      st_id  varchar2(20),
  4      skill    varchar2(20)
  5  );
SQL>  insert into student_skill values('973231','籃球');
SQL>  insert into student_skill(st_id) values('973232');

SQL>  insert into student_skill values('973233','足球');
===================SQL>   select a.* , b.skill from students a,student_skill b where a.st_id=b.st_id(+)
order by a.st_id;ST_ID                NAME             AGE TOL_SCORE SKILL
-------------------- ---------- --------- --------- ------------------  --
973231               wangbindu         22       501        籃球
973232               zhuzhijing           21       538
973233               gaojing                21       576        足球1.3自我連接
自我連接是在同一個表或視圖內(nèi)進行條件連接.
[例]自我連接實例
查詢每個雇員的名字和該雇員的經(jīng)理的名字:
SQL> select e1.ename'   work for   'e2.ename "Employees and their Managers"
  2  from  scott.emp e1,scott.emp e2 where e1.mgr=e2.empno;Employees and their Managers
-------------------------------------------------
SMITH      work for   FORD
ALLEN     work for   BLAKE
WARD      work for   BLAKE
JONES     work for   KING
MARTIN   work for   BLAKE
BLAKE     work for   KING
CLARK     work for   KING
SCOTT      work for   JONES
TURNER  work for   BLAKE
ADAMS     work for   SCOTT
JAMES      work for   BLAKE
FORD        work for   JONES
MILLER     work for   CLARK1.4UNION , INTERSECT及 MINUS
UNION:            可以將兩個以上的表的相類似的查詢結(jié)果放在一起 (union all則表示返回所有的行)
具體語法:
select ...
union[all]
select...
==========INTERSECT:  返回兩個表中相同的信息
具體語法:
select ...
intersect
select...
==========MINUS          :  返回一個表中出現(xiàn)的信息
具體語法:
select ...
minus
select...
[例1]UNION操作實例
SQL> select  st_id  from students
  2  union
  3  select  st_id  from student_skill;ST_ID
--------------------

973231
973232
973233[例2]INTERSECT操作實例
列出有特長的學(xué)生的學(xué)號
SQL> select st_id from students
  2  intersect
  3  select st_id from student_skill;
ST_ID
--------------------
973231
973233[例3]MINUS操作實例
列出沒有特長學(xué)生的學(xué)號
select st_id from students
minus
select st_id from student_skill;
ST_ID
--------------------
973232
2.創(chuàng)建復(fù)雜的視圖
許多應(yīng)用系統(tǒng)有統(tǒng)計等功能,建議最好把這些復(fù)雜語句寫成視圖.下面是幾個常用的視圖.
2.1分組視圖
[例1]簡單的分組視圖
SQL> create or replace view dept_tot as
  2  select a.dname dept,sum(b.sal) total_sal from scott.dept a,scott.emp b
  3  where a.deptno=b.deptno group by a.dname;查看已建立。
SQL> select * from dept_tot;DEPT                           TOTAL_SAL
--------------                     ---------
ACCOUNTING          8750
RESEARCH              10875
SALES                        9400[例2]帶復(fù)雜函數(shù)視圖
SQL> create or replace view itemtot as
  2  select persion,sum(amount) itemtot from ledger
  3  where actiondate between
  4  to_date('01-MAR-1901','dd-mon-yyyy') and
  5  to_date('31-MAR-1901','dd-mon-yyyy')
  6  and action in('bought','raid') group by persion;2.2合計視圖
[例]合計函數(shù)視圖實例
SQL> create or replace view emp_no1  as
  2  select deptno,sum(sal) 工資和,sum(comm) 總和
  3  from scott.emp group by deptno;
SQL> select * from emp_no1;
DEPTNO    工資和      總和
--------- --------- ---------
       10      8750
       20     10875
       30      9400      22002.3組合視圖
[例]帶組合函數(shù)的視圖
SQL> create or replace view byitem as
  2  select l.persion persion.item, amount, 100*amount/item bypersion,100*amount/total bytotal
  3  from ledgent l,itemtotal i,total where l.persion=i.persion where l.persion=i.persion
  4  and actiondate between
  5  to_date('01-MAR-1901','dd-mon-yyyy') and
  6  to_date('31-MAR-1901','dd-mon-yyyy')
  7   and action in('bought','raid') ;3.家族樹
語法:
select column from table_name start with column=value
connect by PRior 父主鍵=子主鍵3.1排除單一性和分枝
以O(shè)racle中的EMP表為例

[例]從頂?shù)降琢谐龈鞴蛦T的信息
SQL> select lpad(' ',4*(level-1))ename name,empno,mgr from emp start with mgr is null
  2  connect by prior empno=mgr;NAME                                 EMPNO       MGR
---------                                  ---------           ---------
KING                                   7839           
    JONES                           7566            7839
          SCOTT                     7788            7566
                ADAMS              7876            77883.2遍歷至根
[例1]現(xiàn)在要從某個雇員開始向他的上級列出該雇員的層次結(jié)構(gòu)
SQL> col ename for a30;
SQL> select lpad(' ',4*(level-1))ename ename,mgr,empno from scott.emp
  2  start with mgr=7788 connect by prior mgr=empno;
ENAME                                MGR     EMPNO
------------------------------         ---------    ---------
ADAMS                               7788      7876
    SCOTT                           7566       7788
        JONES                       7839       7566

            KING                                        7839[例2]列出所有雇員的層次結(jié)構(gòu)
SQL> select lpad(' ',4*(level-1))ename ename,empno,mgr from scott.emp
  2  start with mgr is not null
  3  connect by empno=prior mgr;ENAME                              EMPNO       MGR
------------------------------       ---------           ---------
SMITH                               7369          7902
    FORD                            7902         7566
        JONES                       7566        7839
            KING                    7839
ALLEN                               7499        7698
    BLAKE                           7698        7839
        KING                        7839
WARD                                7521        7698
    BLAKE                           7698        7839
        KING                        7839

JONES                               7566        7839
    KING                            7839
MARTIN                              7654        7698
    BLAKE                           7698         7839
        KING                        7839
BLAKE                               7698        7839
    KING                            7839
CLARK                               7782         7839
    KING                            7839
SCOTT                               7788         7566
    JONES                           7566        7839

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 大庆市| 峨眉山市| 克拉玛依市| 青龙| 普安县| 通榆县| 三原县| 天津市| 绥化市| 平潭县| 江都市| 都江堰市| 札达县| 哈密市| 双辽市| 凯里市| 兰考县| 乌兰浩特市| 石景山区| 镶黄旗| 抚宁县| 普兰店市| 东方市| 沈阳市| 平和县| 长垣县| 横山县| 黑河市| 霞浦县| 陇西县| 齐齐哈尔市| 上林县| 云南省| 安福县| 横峰县| 伊吾县| 苏尼特右旗| 乳源| 南靖县| 新邵县| 波密县|