濮阳杆衣贸易有限公司

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

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

熱門標簽:貴陽智能電銷機器人官網(wǎng) 北京營銷外呼系統(tǒng)廠家 溫州人工外呼系統(tǒng) 外呼系統(tǒng)鄭州 百度地圖標注員是干什么 沈陽400電話是如何辦理 地圖標注付款了怎么找不到了 北京外呼系統(tǒng)公司排名 外呼系統(tǒng)口號
大部分情況下,這種動態(tài)生成的sql查詢語句寫法如下:
復制代碼 代碼如下:

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

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

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

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

首先,建立三個表,一個反應學生基本情況的信息表——student表,兩個存放學生相關(guān)信息的代碼表——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 '名稱';

然后,執(zhí)行以下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查詢方式和本文倡導的查詢方式及其查詢結(jié)果比較:
通用查詢方式及其查詢結(jié)果如下:
復制代碼 代碼如下:

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é)果如下

復制代碼 代碼如下:

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

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

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

標簽:通遼 包頭 定西 潮州 溫州 衡水 淮北 衢州

巨人網(wǎng)絡通訊聲明:本文標題《在oracle 數(shù)據(jù)庫查詢的select 查詢字段中關(guān)聯(lián)其他表的方法》,本文關(guān)鍵詞  在,oracle,數(shù)據(jù)庫,查詢,的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《在oracle 數(shù)據(jù)庫查詢的select 查詢字段中關(guān)聯(lián)其他表的方法》相關(guān)的同類信息!
  • 本頁收集關(guān)于在oracle 數(shù)據(jù)庫查詢的select 查詢字段中關(guān)聯(lián)其他表的方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    泽普县| 上高县| 正宁县| 铜山县| 建平县| 磐石市| 贵港市| 洛浦县| 陆丰市| 同心县| 治多县| 那坡县| 邯郸市| 当涂县| 梨树县| 盐山县| 杨浦区| 永登县| 锡林郭勒盟| 阿图什市| 高青县| 凌云县| 阿勒泰市| 正宁县| 阳山县| 元谋县| 和平区| 错那县| 绥中县| 东兴市| 溧水县| 崇礼县| 莒南县| 繁峙县| 武宁县| 瑞昌市| 夏津县| 突泉县| 孝义市| 恩施市| 和静县|