Home Database Mysql Tutorial Oracle 中的JOIN

Oracle 中的JOIN

Jun 07, 2016 pm 03:25 PM
join oracle Overview

1、概述 1.1、所有的join连接,都可以加上类where a.id='1000'的条件,达到同样的效果。 1.2、除了cross join不可以加on外,其它join连接都必须加上on关键字,后都可加where条件。 1.3、虽然都可以加where条件,但是他们只在标准连接的结果集上查找where条件

1、概述

1.1、所有的join连接,都可以加上类似where a.id='1000'的条件,达到同样的效果。

1.2、除了cross join不可以加on外,其它join连接都必须加上on关键字,后都可加where条件。

1.3、虽然都可以加where条件,但是他们只在标准连接的结果集上查找where条件。比如左外连接的结果没有class的三班,所以如果加 where class.id='C003'虽然在表中有,但在左连接结果集中没有,所以查询后,是没有记录的。

2、实例,标准的join连接,(不加where条件的)

2.1、设有表如下:

学生表

Oracle 中的JOIN      

班级表,对应学生表中的classid

Oracle 中的JOIN

2.2、自连接:join ,inner join

Oracle 中的JOIN

<span>1</span> <span>--</span><span>自连接  :只返回两张表连接列的匹配项。</span><span>2</span> <span>--</span><span>以下三种查询结果一样。</span><span>3</span> <span>select</span> <span>*</span> <span>from</span> student s <span>inner</span> <span>join</span> class c <span>on</span> s.classid<span>=</span>c.id; 
<span>4</span> <span>select</span> <span>*</span> <span>from</span> student s <span>join</span> class c <span>on</span> s.classid<span>=</span>c.id;
<span>5</span> <span>select</span> <span>*</span> <span>from</span> student s,class c <span>where</span> s.classid<span>=</span>c.id;
Copy after login

Oracle 中的JOIN

自连接结果:

Oracle 中的JOIN

2.3、笛卡儿乘积:cross join

Oracle 中的JOIN

<span>1</span> <span>--</span><span>笛卡儿乘积连接 :即不加任何条件,达到 M*N 的结果集。</span><span>2</span> <span>--</span><span>以下两种查询结果一样。</span><span>3</span> <span>select</span> <span>*</span> <span>from</span> student s <span>cross</span> <span>join</span> class c;
<span>4</span> <span>select</span> <span>*</span> <span>from</span> student,class;
Copy after login

Oracle 中的JOIN

笛卡尔结果:

Oracle 中的JOIN

注意:如果cross join加上where s.classid=c.id条件,会产生跟自连接一样的结果:

Oracle 中的JOIN

<span>1</span> <span>--</span><span>加上条件,产生跟自连接一样的结果。</span><span>2</span> <span>select</span> <span>*</span> <span>from</span> student s <span>cross</span> <span>join</span> class c <span>where</span> s.classid<span>=</span>c.id;
Copy after login

Oracle 中的JOIN

自连接结果集的cross join连接结果

Oracle 中的JOIN

2.3、左外连接:left join 

Oracle 中的JOIN

<span>1</span> <span>--</span><span>左连接 :列出左边表全部的,及右边表符合条件的,不符合条件的以空值代替。</span><span>2</span> <span>--</span><span>在(+)计算时,哪个带(+)哪个需要条件符合的,另一个全部的。即放左即右连接,放右即左连接。</span><span>3</span> <span>--</span><span>以下结果集相同。</span><span>4</span> <span>select</span> <span>*</span> <span>from</span> student s <span>left</span> <span>join</span> class c <span>on</span> s.classid<span>=</span>c.id;
<span>5</span> <span>select</span> <span>*</span> <span>from</span> student s,class c <span>where</span> s.classid<span>=</span>c.id(<span>+</span>);
Copy after login

Oracle 中的JOIN

左连接结果:

Oracle 中的JOIN

2.4、右外连接:right join

Oracle 中的JOIN

<span>1</span> <span>--</span><span>右外连接 :与左连接一样,列出右边表全部的,及左边表符合条件的,不符合条件</span><span>2</span> <span>--</span><span>的用 空值  替代。</span><span>3</span> <span>--</span><span>(+)一样,它的位置与连接相反。</span><span>4</span> <span>select</span> <span>*</span> <span>from</span> student s <span>right</span> <span>join</span> class c <span>on</span> s.classid<span>=</span>c.id;
<span>5</span> <span>select</span> <span>*</span> <span>from</span> student s,class c <span>where</span> s.classid(<span>+</span>)<span>=</span>c.id;
Copy after login

Oracle 中的JOIN

右连接结果

Oracle 中的JOIN

2.5、全连接:full join

Oracle 中的JOIN

<span>1</span> <span>--</span><span>全连接 :产生M+N的结果集,列出两表全部的,不符合条件的,以空值代替。</span><span>2</span> <span>select</span> <span>*</span> <span>from</span> student s <span>full</span> <span>join</span> class c <span>on</span> s.classid<span>=</span>c.id;
Copy after login

Oracle 中的JOIN

全连接结果集

Oracle 中的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)

Hot Topics

Java Tutorial
1657
14
PHP Tutorial
1257
29
C# Tutorial
1230
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 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.

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

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.

How to create oracle dynamic sql How to create oracle dynamic sql Apr 12, 2025 am 06:06 AM

SQL statements can be created and executed based on runtime input by using Oracle's dynamic SQL. The steps include: preparing an empty string variable to store dynamically generated SQL statements. Use the EXECUTE IMMEDIATE or PREPARE statement to compile and execute dynamic SQL statements. Use bind variable to pass user input or other dynamic values ​​to dynamic SQL. Use EXECUTE IMMEDIATE or EXECUTE to execute dynamic SQL statements.

See all articles