在oracle 資料庫查詢的select 查詢欄位中關聯其他表的方法_oracle

來源:互聯網
上載者:User
大部分情況下,這種動態產生的sql查詢語句寫法如下:
複製代碼 代碼如下:

select A表.欄位1,A表.欄位2,B表.欄位返回,C表.欄位返回 from A表 ,B表,C表 [where A表,B表,C表關聯及各自的條件陳述式]

但是這個方法有一個缺點,那就是在動態產生這個查詢語句的商務邏輯程式仍然很複雜。這裡就介紹一個降低商務邏輯複雜度的查詢sql產生方式。其文法結構如下:
複製代碼 代碼如下:

select A表.欄位1,A表.欄位2,B表.欄位,C表.欄位 from A表 [where A表的條件陳述式]

商務邏輯程式通過這種方式產生的sql語句時只需修改select的欄位,而不需像通用方法那樣需要同時動態修改select欄位,from的表,以及where 語句。這樣真箇商務邏輯就能將產生sql語句的關注點由3+個減少為1個。下面就該方式實現舉例如下:

首先,建立三個表,一個反應學生基本情況的資訊表——student表,兩個存放學生相關資訊的代碼錶——sexCode表(性別代碼錶),gradeCode(年紀代碼錶),建表語句如下:
複製代碼 代碼如下:

-- Create table STUDENT
create table STUDENT
(
ID number,
name nvarchar2(10),
sex char(1),
grade char(1),
age number(2)
)
tablespace SDMP
storage
(
initial 64K
minextents 1
maxextents unlimited
);
-- Add comments to the columns
comment on column STUDENT.name
is '學生姓名';
comment on column STUDENT.sex
is '學生性別';
comment on column STUDENT.grade
is '年級';
comment on column STUDENT.age
is '年齡';

複製代碼 代碼如下:

-- Create table SEXCODE
create table SEXCODE
(
DM char(1),
MC nvarchar2(5)
)
tablespace SDMP
storage
(
initial 64K
minextents 1
maxextents unlimited
);
-- Add comments to the columns
comment on column SEXCODE.DM
is '代碼';
comment on column SEXCODE.MC
is '名稱';

複製代碼 代碼如下:

-- Create table GRADECODE
create table GRADECODE
(
DM CHAR(1),
MC NVARCHAR2(5)
)
tablespace SDMP
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
-- Add comments to the columns
comment on column GRADECODE.DM
is '代碼';
comment on column GRADECODE.MC
is '名稱';

然後,執行以下insert語句,分別在每個表中填入資訊。
複製代碼 代碼如下:

--insert into student
insert into student(id,name,sex,grade,age) values(1,'張三','1','2',8);
insert into student(id,name,sex,grade,age) values(2,'李四','0','1',11);
insert into student(id,name,sex,grade,age) values(3,'王五','1','2',9);
insert into student(id,name,sex,grade,age) values(4,'劉二','0','4',8);
insert into student(id,name,sex,grade,age) values(5,'韓六','0','3',6);

--insert into sexcode
insert into sexcode(dm,mc) values('1','男');
insert into sexcode(dm,mc) values('0','女');

--insert into gradecode
insert into gradecode(dm,mc) values('1','一年級');
insert into gradecode(dm,mc) values('2','二年級');
insert into gradecode(dm,mc) values('3','三年級');

最後,給出常用sql查詢方式和本文倡導的查詢方式及其查詢結果比較:
通用查詢方式及其查詢結果如下:
複製代碼 代碼如下:

select s.id,s.name,sc.mc sex,gc.mc grade,s.age
from student s,sexcode sc,gradecode gc
where sc.dm=s.sex(+) and s.grade=gc.dm(+)

ID NAME SEX GRADE AGE
1 2 李四 一年級 11
2 3 王五 二年級 9
3 1 張三 二年級 8
4 5 韓六 三年級 6
5 4 劉二 8

本問題出查詢方法及其查詢結果如下

複製代碼 代碼如下:

select s.id,s.name,s.age,
(select mc from sexcode where dm=s.sex) sex,
(select mc from gradecode where dm=s.grade) grade
from student s

ID NAME AGE SEX GRADE
1 1 張三 8 二年級
2 2 李四 11 一年級
3 3 王五 9 二年級
4 4 劉二 8
5 5 韓六 6 三年級

註:1.對於二者的效能,這裡只是做了個簡單測試,1000條資料查詢耗時二者相當,而且本文提到方法甚至略優於普通方法。

2.此方法目前只在oracle資料庫中實現並測試,其他資料庫請自行測試。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.