search
HomeDatabaseMysql Tutorial在oracle 数据库查询的select 查询字段中关联其他表的方法

在实际开发过程中,特别是在做数据查询的时候,能够根据动态生成的sql语句将查询的结果转化并返回到业务处理逻辑(或页面展示出来)能大大的减轻业务逻辑的处理复杂度。

大部分情况下,这种动态生成的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(+)

本问题出查询方法及其查询结果如下
代码如下:
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

注:1.对于二者的性能,这里只是做了个简单测试,1000条数据查询耗时二者相当,而且本文提到方法甚至略优于普通方法。

2.此方法目前只在oracle数据库中实现并测试,其他数据库请自行测试。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Adding Users to MySQL: The Complete TutorialAdding Users to MySQL: The Complete TutorialMay 12, 2025 am 12:14 AM

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

Mastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMay 12, 2025 am 12:12 AM

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

MySQL: String Data Types and Indexing: Best PracticesMySQL: String Data Types and Indexing: Best PracticesMay 12, 2025 am 12:11 AM

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

MySQL: How to Add a User RemotelyMySQL: How to Add a User RemotelyMay 12, 2025 am 12:10 AM

ToaddauserremotelytoMySQL,followthesesteps:1)ConnecttoMySQLasroot,2)Createanewuserwithremoteaccess,3)Grantnecessaryprivileges,and4)Flushprivileges.BecautiousofsecurityrisksbylimitingprivilegesandaccesstospecificIPs,ensuringstrongpasswords,andmonitori

The Ultimate Guide to MySQL String Data Types: Efficient Data StorageThe Ultimate Guide to MySQL String Data Types: Efficient Data StorageMay 12, 2025 am 12:05 AM

TostorestringsefficientlyinMySQL,choosetherightdatatypebasedonyourneeds:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseTEXTforlong-formtextcontent.4)UseBLOBforbinarydatalikeimages.Considerstorageov

MySQL BLOB vs. TEXT: Choosing the Right Data Type for Large ObjectsMySQL BLOB vs. TEXT: Choosing the Right Data Type for Large ObjectsMay 11, 2025 am 12:13 AM

When selecting MySQL's BLOB and TEXT data types, BLOB is suitable for storing binary data, and TEXT is suitable for storing text data. 1) BLOB is suitable for binary data such as pictures and audio, 2) TEXT is suitable for text data such as articles and comments. When choosing, data properties and performance optimization must be considered.

MySQL: Should I use root user for my product?MySQL: Should I use root user for my product?May 11, 2025 am 12:11 AM

No,youshouldnotusetherootuserinMySQLforyourproduct.Instead,createspecificuserswithlimitedprivilegestoenhancesecurityandperformance:1)Createanewuserwithastrongpassword,2)Grantonlynecessarypermissionstothisuser,3)Regularlyreviewandupdateuserpermissions

MySQL String Data Types Explained: Choosing the Right Type for Your DataMySQL String Data Types Explained: Choosing the Right Type for Your DataMay 11, 2025 am 12:10 AM

MySQLstringdatatypesshouldbechosenbasedondatacharacteristicsandusecases:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseBINARYorVARBINARYforbinarydatalikecryptographickeys.4)UseBLOBorTEXTforlargeuns

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.