Home Database Mysql Tutorial Oracle 连接查询

Oracle 连接查询

Jun 07, 2016 pm 03:37 PM
oracle Inquire background Record connect

背景: 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
Copy after login
   语句2:显式的交叉连接,使用CROSS JOIN

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

二、内连接

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

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

1. 相等连接

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

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

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

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/imghw/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
Copy after login
结果如下图:

Oracle 连接查询

三、外连接

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

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

1、  左外连接

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

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

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

        在这个例子中,首先要保证的是:我要查出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 
Copy after login

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

比如:

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

2、  右外连接

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

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

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

Oracle 连接查询

3、  全连接

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

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

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

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 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 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)

How to encrypt oracle view How to encrypt oracle view Apr 11, 2025 pm 08:30 PM

Oracle View Encryption allows you to encrypt data in the view, thereby enhancing the security of sensitive information. The steps include: 1) creating the master encryption key (MEk); 2) creating an encrypted view, specifying the view and MEk to be encrypted; 3) authorizing users to access the encrypted view. How encrypted views work: When a user querys for an encrypted view, Oracle uses MEk to decrypt data, ensuring that only authorized users can access readable data.

How to uninstall Oracle installation failed How to uninstall Oracle installation failed Apr 11, 2025 pm 08:24 PM

Uninstall method for Oracle installation failure: Close Oracle service, delete Oracle program files and registry keys, uninstall Oracle environment variables, and restart the computer. If the uninstall fails, you can uninstall manually using the Oracle Universal Uninstall Tool.

How to delete all data from oracle How to delete all data from oracle Apr 11, 2025 pm 08:36 PM

Deleting all data in Oracle requires the following steps: 1. Establish a connection; 2. Disable foreign key constraints; 3. Delete table data; 4. Submit transactions; 5. Enable foreign key constraints (optional). Be sure to back up the database before execution to prevent data loss.

How to check invalid numbers of oracle How to check invalid numbers of oracle Apr 11, 2025 pm 08:27 PM

Oracle Invalid numeric errors may be caused by data type mismatch, numeric overflow, data conversion errors, or data corruption. Troubleshooting steps include checking data types, detecting digital overflows, checking data conversions, checking data corruption, and exploring other possible solutions such as configuring the NLS_NUMERIC_CHARACTERS parameter and enabling data verification logging.

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 paginate oracle database How to paginate oracle database Apr 11, 2025 pm 08:42 PM

Oracle database paging uses ROWNUM pseudo-columns or FETCH statements to implement: ROWNUM pseudo-columns are used to filter results by row numbers and are suitable for complex queries. The FETCH statement is used to get the specified number of first rows and is suitable for simple queries.

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.

See all articles