SQL 连接查询语法及使用
一、交叉连接(cross join) 交叉连接(cross join):有两种,显式的和隐式的,不带on子句,返回的是两表的乘积,也叫笛卡尔积。 例如:下面的语句1和语句2的结果是相同的。 语句1:隐式的交叉连接,没有cross join. select o.id, o.order_number, c.id, c.name
一、交叉连接(cross join)
交叉连接(cross join):有两种,显式的和隐式的,不带on子句,返回的是两表的乘积,也叫笛卡尔积。
例如:下面的语句1和语句2的结果是相同的。
语句1:隐式的交叉连接,没有cross join.
select o.id, o.order_number, c.id, c.name
from orders o , customers c
where o.id=1;
语句2:显式的交叉连接,使用cross join.
select o.id,o.order_number,c.id,c.name
from orders o cross join customers c
where o.id=1;
语句1和语句2的结果是相同的,查询结果如下:
二、内连接(inner join)
内连接(inner join):有两种,显式的和隐式的,返回连接表中符合连接条件和查询条件的数据行。(所谓的链接表就是数据库在做查询形成的中间表)。
例如:下面的语句3和语句4的结果是相同的。
语句3:隐式的内连接,没有inner join,形成的中间表为两个表的笛卡尔积。
select o.id,o.order_number,c.id,c.name
from customers c, orders o
where c.id=o.customer_id;
语句4:显示的内连接,一般称为内连接,有inner join,形成的中间表为两个表经过on条件过滤后的笛卡尔积。
select o.id,o.order_number,c.id,c.name
from customers c inner join orders o on c.id=o.customer_id;
语句3和语句4的查询结果:
三、外连接(outer join):
外连不但返回符合连接和查询条件的数据行,还返回不符合条件的一些行。外连接分三类:左外连接(left outer join)、右外连接(right outer join)和全外连接(full outer join)。
三者的共同点是都返回符合连接条件和查询条件(即:内连接)的数据行。不同点如下:
左外连接还返回左表中不符合连接条件单符合查询条件的数据行。
右外连接还返回右表中不符合连接条件单符合查询条件的数据行。
全外连接还返回左表中不符合连接条件单符合查询条件的数据行,并且还返回右表中不符合连接条件单符合查询条件的数据行。全外连接实际是上左外连接和右外连接的数学合集(去掉重复),即"全外=左外union 右外".
说明:左表就是在"(left outer join)"关键字左边的表。右表当然就是右边的了。在三种类型的外连接中,outer 关键字是可省略的。
下面举例说明:
语句5:左外连接(left outer join)
select o.id,o.order_number,o.customer_id,c.id,c.name
from orders o left outer join customers c on c.id=o.customer_id;
语句6:右外连接(right outer join)
select o.id,o.order_number,o.customer_id,c.id,c.name
from orders o right outer join customers c on c.id=o.customer_id;
注意:where条件放在on后面查询的结果是不一样的。例如:
语句7:where条件独立。
select o.id,o.order_number,o.customer_id,c.id,c.name
from orders o left outer join customers c on c.id=o.customer_id
where o.order_number'mike_order001';
语句8:将语句7中的where条件放到on后面。
select o.id,o.order_number,o.customer_id,c.id,c.name
from orders o left outer join customers c on c.id=o.customer_id and o.order_number'mike_order001';
从语句7和语句8查询的结果来看,显然是不相同的,语句8显示的结果是难以理解的。因此,推荐在写连接查询的时候,on后面只跟连接条件,而对中间表限制的条件都写到where子句中。
语句9:全外连接(full outer join)。
select o.id,o.order_number,o.customer_id,c.id,c.name
from orders o full outer join customers c on c.id=o.customer_id;
注意:mysql是不支持全外的连接的,这里给出的写法适合oracle和db2.但是可以通过左外和右外求合集来获取全外连接的查询结果。下图是上面sql在oracle下执行的结果:
语句10:左外和右外的合集,实际上查询结果和语句9是相同的。
select o.id,o.order_number,o.customer_id,c.id,c.name
from orders o left outer join customers c on c.id=o.customer_id
union
select o.id,o.order_number,o.customer_id,c.id,c.name
from orders o right outer join customers c on c.id=o.customer_id;
语句9和语句10的查询结果是相同的,如下:
四、联合连接(union join):
这是一种很少见的连接方式。oracle、mysql均不支持,其作用是:找出全外连接和内连接之间差异的所有行。这在数据分析中排错中比较常用。也可以利用数据库的集合操作来实现此功能。
语句11:联合查询(union join)例句,还没有找到能执行的sql环境。
select o.id,o.order_number,o.customer_id,c.id,c.name
from orders o union join customers c on c.id=o.customer_id
语句12:语句11在db2下的等价实现。还不知道db2是否支持语句11呢!
select o.id,o.order_number,o.customer_id,c.id,c.name
from orders o full outer join customers c on c.id=o.customer_id
except
select o.id,o.order_number,o.customer_id,c.id,c.name
from orders o inner join customers c on c.id=o.customer_id;
语句13:语句11在oracle下的等价实现。
select o.id,o.order_number,o.customer_id,c.id,c.name
from orders o full outer join customers c on c.id=o.customer_id
minus
select o.id,o.order_number,o.customer_id,c.id,c.name
from orders o inner join customers c on c.id=o.customer_id;
查询结果如下:
五、自然连接(natural inner join):
说真的,这种连接查询没有存在的价值,既然是sql2标准中定义的,就给出个例子看看吧。自然连接无需指定连接列,sql会检查两个表中是否相同名称的列,且假设他们在连接条件中使用,并且在连接条件中仅包含一个连接列。不允许使用on语句,不允许指定显示列,显示列只能用*表示(oracle环境下测 试的)。对于每种连接类型(除了交叉连接外),均可指定natural.下面给出几个例子。
语句14:
select *
from orders o natural inner join customers c;
语句15:
select *
from orders o natural left outer join customers c;
语句16:
select *
from orders o natural right outer join customers c;
语句17:
select *
from orders o natural full outer join customers c;
六、sql查询的基本原理:两种情况介绍。
第一、?? 单表查询:根据where条件过滤表中的记录,形成中间表(这个中间表对用户是不可见的);然后根据select的选择列选择相应的列进行返回最终结果。
第二、?? 两表连接查询:对两表求积(笛卡尔积)并用on条件和连接类型进行过滤形成中间表;然后根据where条件过滤中间表的记录,并根据select指定的列返回查询结果。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



CrystalDiskMark is a small HDD benchmark tool for hard drives that quickly measures sequential and random read/write speeds. Next, let the editor introduce CrystalDiskMark to you and how to use crystaldiskmark~ 1. Introduction to CrystalDiskMark CrystalDiskMark is a widely used disk performance testing tool used to evaluate the read and write speed and performance of mechanical hard drives and solid-state drives (SSD). Random I/O performance. It is a free Windows application and provides a user-friendly interface and various test modes to evaluate different aspects of hard drive performance and is widely used in hardware reviews

foobar2000 is a software that can listen to music resources at any time. It brings you all kinds of music with lossless sound quality. The enhanced version of the music player allows you to get a more comprehensive and comfortable music experience. Its design concept is to play the advanced audio on the computer The device is transplanted to mobile phones to provide a more convenient and efficient music playback experience. The interface design is simple, clear and easy to use. It adopts a minimalist design style without too many decorations and cumbersome operations to get started quickly. It also supports a variety of skins and Theme, personalize settings according to your own preferences, and create an exclusive music player that supports the playback of multiple audio formats. It also supports the audio gain function to adjust the volume according to your own hearing conditions to avoid hearing damage caused by excessive volume. Next, let me help you

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

Download the latest version of 12306 ticket booking app. It is a travel ticket purchasing software that everyone is very satisfied with. It is very convenient to go wherever you want. There are many ticket sources provided in the software. You only need to pass real-name authentication to purchase tickets online. All users You can easily buy travel tickets and air tickets and enjoy different discounts. You can also start booking reservations in advance to grab tickets. You can book hotels or special car transfers. With it, you can go where you want to go and buy tickets with one click. Traveling is simpler and more convenient, making everyone's travel experience more comfortable. Now the editor details it online Provides 12306 users with a way to view historical ticket purchase records. 1. Open Railway 12306, click My in the lower right corner, and click My Order 2. Click Paid on the order page. 3. On the paid page

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

How to check my academic qualifications on Xuexin.com? You can check your academic qualifications on Xuexin.com, but many users don’t know how to check their academic qualifications on Xuexin.com. Next, the editor brings you a graphic tutorial on how to check your academic qualifications on Xuexin.com. Interested users come and take a look! Xuexin.com usage tutorial: How to check your academic qualifications on Xuexin.com 1. Xuexin.com entrance: https://www.chsi.com.cn/ 2. Website query: Step 1: Click on the Xuexin.com address above to enter the homepage Click [Education Query]; Step 2: On the latest webpage, click [Query] as shown by the arrow in the figure below; Step 3: Then click [Login Academic Credit File] on the new page; Step 4: On the login page Enter the information and click [Login];

When you use the Edge browser to access web pages, have you ever encountered a prompt that your connection is not a dedicated connection, causing web browsing to fail? How is this going? Many friends don’t know how to deal with this problem. You can take a look at the following three solutions. Method 1 (simple and crude): In the edge browser, you can try to solve the problem of the website being inaccessible by entering the settings and turning off the security function, and then blocking location permissions in the website permissions. It is important to note that the effectiveness and duration of this approach may vary, and specific effects cannot be determined. After restarting your browser, you can try visiting the website to see if the issue is resolved. Method 2: Adjust the keyboard to English input
