佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > 在oracle 數(shù)據(jù)庫查詢的select 查詢字段中關(guān)聯(lián)其他表的方法

在oracle 數(shù)據(jù)庫查詢的select 查詢字段中關(guān)聯(lián)其他表的方法

熱門標(biāo)簽:地方門戶網(wǎng)站 AI電銷 網(wǎng)站排名優(yōu)化 服務(wù)外包 百度競價排名 呼叫中心市場需求 Linux服務(wù)器 鐵路電話系統(tǒng)
大部分情況下,這種動態(tài)生成的sql查詢語句寫法如下:
復(fù)制代碼 代碼如下:

select A表.字段1,A表.字段2,B表.字段返回,C表.字段返回 from A表 ,B表,C表 [where A表,B表,C表關(guān)聯(lián)及各自的條件語句]

但是這個方法有一個缺點(diǎn),那就是在動態(tài)的生成這個查詢語句的業(yè)務(wù)邏輯程序仍然很復(fù)雜。這里就介紹一個降低業(yè)務(wù)邏輯復(fù)雜度的查詢sql生成方式。其語法結(jié)構(gòu)如下:
復(fù)制代碼 代碼如下:

select A表.字段1,A表.字段2,B表.字段,C表.字段 from A表 [where A表的條件語句]

業(yè)務(wù)邏輯程序通過這種方式生成的sql語句時只需修改select的字段,而不需像通用方法那樣需要同時動態(tài)修改select字段,from的表,以及where 語句。這樣真?zhèn)€業(yè)務(wù)邏輯就能將生成sql語句的關(guān)注點(diǎn)由3+個減少為1個。下面就該方式實(shí)現(xiàn)舉例如下:

首先,建立三個表,一個反應(yīng)學(xué)生基本情況的信息表——student表,兩個存放學(xué)生相關(guān)信息的代碼表——sexCode表(性別代碼表),gradeCode(年紀(jì)代碼表),建表語句如下:
復(fù)制代碼 代碼如下:

-- 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 '學(xué)生姓名';
comment on column STUDENT.sex
is '學(xué)生性別';
comment on column STUDENT.grade
is '年級';
comment on column STUDENT.age
is '年齡';

復(fù)制代碼 代碼如下:

-- 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 '名稱';

復(fù)制代碼 代碼如下:

-- 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 '名稱';

然后,執(zhí)行以下insert語句,分別在每個表中填入信息。
復(fù)制代碼 代碼如下:

--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查詢方式和本文倡導(dǎo)的查詢方式及其查詢結(jié)果比較:
通用查詢方式及其查詢結(jié)果如下:
復(fù)制代碼 代碼如下:

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

本問題出查詢方法及其查詢結(jié)果如下

復(fù)制代碼 代碼如下:

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條數(shù)據(jù)查詢耗時二者相當(dāng),而且本文提到方法甚至略優(yōu)于普通方法。

2.此方法目前只在oracle數(shù)據(jù)庫中實(shí)現(xiàn)并測試,其他數(shù)據(jù)庫請自行測試。

您可能感興趣的文章:
  • 使用SQL語句查詢MySQL,SQLServer,Oracle所有數(shù)據(jù)庫名和表名,字段名
  • Oracle數(shù)據(jù)庫表中字段順序的修改方法
  • Oracle表字段的增刪改、表的重命名及主鍵的增刪改
  • Oracle刪除表、字段之前判斷表、字段是否存在
  • oracle獲取當(dāng)前用戶表、字段等詳細(xì)信息SQL
  • oracle刪除表字段和oracle表增加字段
  • Oracle表字段有Oracle關(guān)鍵字出現(xiàn)異常解決方案

標(biāo)簽:衡水 蘭州 仙桃 黃山 銅川 崇左 湖南 湘潭

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《在oracle 數(shù)據(jù)庫查詢的select 查詢字段中關(guān)聯(lián)其他表的方法》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    阿拉善右旗| 仙桃市| 剑川县| 娄烦县| 临夏市| 清水县| 南溪县| 四平市| 荥经县| 搜索| 富阳市| 龙胜| 景德镇市| 扶风县| 胶南市| 施秉县| 孝昌县| 涪陵区| 出国| 邢台县| 邻水| 偏关县| 方正县| 铁力市| 鲁山县| 海丰县| 舟山市| 嘉禾县| 彝良县| 隆昌县| 浠水县| 湘西| 新营市| 苗栗县| 泾阳县| 漾濞| 海丰县| 鹤岗市| 平远县| 金沙县| 昌宁县|