search
HomeDatabaseMysql TutorialOracle 连接查询

Oracle 连接查询

Jun 07, 2016 pm 03:37 PM
oracleInquirebackgroundRecordconnect

背景: emp表中有14条记录: dept表中4条记录: salgrade表中有5条记录: 一、交叉连接(CROSS JOIN) 交叉连接(CROSS JOIN):有两种,显式的和隐式的,不带ON子句,返回的是两表的乘积,也叫笛卡尔积。 例如:下面的语句1和语句2的结果是相同的(均为56条

背景:

emp表中有14条记录:

Oracle 连接查询

dept表中4条记录:

Oracle 连接查询

salgrade表中有5条记录:

Oracle 连接查询

一、交叉连接(CROSS JOIN)

       交叉连接(CROSS JOIN):有两种,显式的和隐式的,不带ON子句,返回的是两表的乘积,也叫笛卡尔积。

       例如:下面的语句1和语句2的结果是相同的(均为56条记录)。

       语句1:隐式的交叉连接,没有CROSS JOIN。

select empno,ename,sal,dname,loc from emp,dept
   语句2:显式的交叉连接,使用CROSS JOIN

select empno,ename,sal,dname,loc from emp CROSS JOIN dept

二、内连接

       内连接是根据指定的连接条件进行连接查询,只有满足连接条件的数据才会出现在结果集中。

       当执行两个表内连接查询的时,首先在第一个表中查找到第一个记录,然后从头开始扫描第二个表,逐一查找满足条件的记录,找到后将其与第一个表中的的第一个记录拼接形成结果集中的第一个记录。当第二个表被扫描一遍后,再从第一个表中查询第二个记录,然后再从头扫描第二个表,逐一查找满足条件的记录,找到后将其与第一个表中的第二个记录拼接形成结果集中的一个记录。重复执行,知道第一个表中的全部记录都处理完毕为止。

1. 相等连接

       通过两个表具有相同意义的列,可以建立相等连接条件。只有连接列上在两个表中都出现且值相等的行才会出现在查询结果中。

       例如,查询10号部门员工的员工号、员工名、工资、部门号和部门名:

SELECT empno,ename,sal,emp.deptno FROM EMP
join DEPT on EMP.DEPTNO = DEPT.DEPTNO and EMP.DEPTNO=10
       结果如下图:

Oracle 连接查询

2、不相等连接

    如果连接条件中的运算符不是等号而是其他关系的运算符,这成为不相等连接。

    例如,查询10号部门员工的工资等级:

select empno,ename,sal,grade 
from emp 
join salgrade 
on sal>losal and sal<hisal and deptno="10</pre">

<p><span><span>结果如下图:</span><br>
</span></p>
<span><img  src="/static/imghwm/default1.png" data-src="/inc/test.jsp?url=http%3A%2F%2Fimg.blog.csdn.net%2F20140208162745703%3Fwatermark%2F2%2Ftext%2FaHR0cDovL2Jsb2cuY3Nkbi5uZXQvamlhbmt1bmtpbmc%3D%2Ffont%2F5a6L5L2T%2Ffontsize%2F400%2Ffill%2FI0JBQkFCMA%3D%3D%2Fdissolve%2F70%2Fgravity%2FSouthEast&refer=http%3A%2F%2Fblog.csdn.net%2Fxunzaosiyecao%2Farticle%2Fdetails%2F18988167" class="lazy" alt="Oracle 连接查询" ></span><br>
<p><span>3、自身连接</span></p>
<p>     <strong>  自身连接是指在同一个表或者视图中进行连接,相当于同一个表作为两个或多个表使用。</strong></p>
<p>       例如:查询所有员工的员工号、员工名与该员工领导的员工名、员工号:</p>

<pre class="brush:php;toolbar:false">select work.empno,work.ename,manager.empno,manager.ename 
from emp work 
join emp manager 
on work.mgr=manager.empno
结果如下图:

Oracle 连接查询

三、外连接

       外连接是指在内连接的基础上,将某个连接表中不符合连接条件的记录加入到结果集中。

       在左外连接和右外连接时都会以一张表为基表,该表的内容会全部显示,然后加上两张表匹配的内容。 如果基表的数据在另一张表没有记录。 那么在相关联的结果集行中列显示为空值(NULL)。

1、  左外连接

       左外连接是指在内连接的基础上,将连接操作符左侧表中不符合连接条件的记录加入到结果集中,与之对应的连接操作符右侧表列用NULL填充。

       例如,查询10号部门的部门名 、员工号、员工名和所有其他部门的名称:

select dname,empno,ename 
from dept 
left join emp 
on dept.deptno=emp.deptno 
and dept.deptno=10;

        在这个例子中,首先要保证的是:我要查出10号部门的部门名称,即使10号部门中一个员工也没有,也要显示10号部门所有的部门名称,而部门名称在dept表中,故dept表放在左侧,有匹配数据的前三行就是用Join时,查出的数据,即满足On条件的数据。

结果如下图:

Oracle 连接查询

小注:关于left join的个人理解

        假如说你有两个表,资金结算单据(JSDJXX)、位信息(DWXX)
        一个表用来存放资金结算单据的信息(资金结算单据中肯定有单位嘛,但资金结算单据中得单位信息肯定是编号),另一个表用来存放单位信息(单位表里的数据比如会有:单位编号、单位名称、单位内码等等),现在我要查资金结算单据的信息,同时我又想让资金结算单据中的单位信息不显示编号,而是显示单位名称,这时候,我就需要用left join来关联单位信息表,因为我需要的数据是资金结算单据中的信息,即使在单位信息表表查不到某个单位编号对应的单位名称,这条数据我还是要显示的嘛,所以用left join而不是join。

SELECT JSDJXX_DWBH,DWXX_DWMC  
from  JSDJXX
left JOIN DWXX ON JSDJXX_DWBH = DWXX_DWBH 

        通过执行上面的SQL,获取ds,我就可以在往Gridcontrol上绑定数据的时候,绑定单位名称。

比如:

SELECT JSDJXX_DWBH,DWXX_DWMC as DWMC
from  JSDJXX
left JOIN DWXX ON JSDJXX_DWBH = DWXX_DWBH 
        在Dev  Gridcontrol上绑定列名的时候就可以用DWMC,即可。

2、  右外连接

       右外连接是指在内连接的基础上,将连接操作符右侧表中不符合连接条件的记录加入到结果集中,与之对应的连接操作符左侧表列用NULL填充。

       例如,查询20号部门的部门名称及其员工号、员工名和所有其他部门的员工号、员工名:

select empno ,ename,dname 
from dept 
right join emp 
on dept.deptno=emp.deptno 
and dept.deptno=20;
结果如下图:

Oracle 连接查询

3、  全连接

       全外连接是指在内外连接的基础上,将连接操作符两侧表中不符合的记录加入到结果集中

       例如,查询所有的部门名和员工名:

select dname,ename 
from emp 
full join dept 
on emp.deptno=dept.deptno
结果如下图:

Oracle 连接查询
小注:

1、左右外连接小结:

      左外连接(LEFT OUTER JOIN)告诉DBMS生成的结果表中,除了包括匹配行外,还包括JOIN关键字(FROM子句中)左边表的不匹配行。左外连接实际上可以表示为:
      左外连接 = 内连接 + 左边表中失配的元组
      其中,缺少的右边表中的属性值用NULL表示。
      右外连接(RIGHT OUTER JOIN)告诉DBMS生成的结果表中,除了包括匹配行外,还包括JOIN关键字(FROM子句中)右边表的不匹配行。右外连接实际上可以表示为:
      右外连接 = 内连接 + 右边表中失配的元组
      其中,缺少的左边表中的属性值用NULL表示。
      全外连接(FULL OUTER JOIN)告诉DBMS生成的结果表中,除了包括匹配行外,还包括JOIN关键字(FROM子句中)左边表和右边表的不匹配行。全外连接实际上可以表示为:
      全外连接 = 内连接 + 左边表中失配的元组 + 右边表中失配的元组。
      其中,缺少的左边表或者右边表中的属性值用NULL表示。

2、类似文章推荐:点击打开链接

3、小结

      下面列出了您可以使用的 JOIN 类型,以及它们之间的差异。

  • JOIN: 如果表中有至少一个匹配,则返回行。例子:点击打开链接
  • LEFT JOIN: 即使右表中没有匹配,也从左表返回所有的行。例子:点击打开链接
  • RIGHT JOIN: 即使左表中没有匹配,也从右表返回所有的行。例子:点击打开链接
  • FULL JOIN: 只要其中一个表中存在匹配,就返回行。例子:点击打开链接
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
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

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 Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools