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

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

Jun 07, 2016 pm 06:06 PM
oracle Database query

在实际开发过程中,特别是在做数据查询的时候,能够根据动态生成的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 of this Website
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

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

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
What to do if the oracle can't be opened What to do if the oracle can't be opened Apr 11, 2025 pm 10:06 PM

Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

How to solve the problem of closing oracle cursor How to solve the problem of closing oracle cursor Apr 11, 2025 pm 10:18 PM

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

How to create cursors in oracle loop How to create cursors in oracle loop Apr 12, 2025 am 06:18 AM

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

What steps are required to configure CentOS in HDFS What steps are required to configure CentOS in HDFS Apr 14, 2025 pm 06:42 PM

Building a Hadoop Distributed File System (HDFS) on a CentOS system requires multiple steps. This article provides a brief configuration guide. 1. Prepare to install JDK in the early stage: Install JavaDevelopmentKit (JDK) on all nodes, and the version must be compatible with Hadoop. The installation package can be downloaded from the Oracle official website. Environment variable configuration: Edit /etc/profile file, set Java and Hadoop environment variables, so that the system can find the installation path of JDK and Hadoop. 2. Security configuration: SSH password-free login to generate SSH key: Use the ssh-keygen command on each node

What to do if the oracle log is full What to do if the oracle log is full Apr 12, 2025 am 06:09 AM

When Oracle log files are full, the following solutions can be adopted: 1) Clean old log files; 2) Increase the log file size; 3) Increase the log file group; 4) Set up automatic log management; 5) Reinitialize the database. Before implementing any solution, it is recommended to back up the database to prevent data loss.

Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

How to export oracle view How to export oracle view Apr 12, 2025 am 06:15 AM

Oracle views can be exported through the EXP utility: Log in to the Oracle database. Start the EXP utility, specifying the view name and export directory. Enter export parameters, including target mode, file format, and tablespace. Start exporting. Verify the export using the impdp utility.

How to stop oracle database How to stop oracle database Apr 12, 2025 am 06:12 AM

To stop an Oracle database, perform the following steps: 1. Connect to the database; 2. Shutdown immediately; 3. Shutdown abort completely.

See all articles