Table of Contents
内连接
语法 
示例
3 多表内连接语法
外连接
左外连接语法
右外连接语法
完全连接
交叉联接
Home Database Mysql Tutorial 数据库系列之查询(5)

数据库系列之查询(5)

Jun 07, 2016 pm 03:00 PM
from select List database Inquire series grammar connect

内连接 1 语法 SELECT 列名 FROM 表1 INNER JOIN 表2 ON 表1.列名 条件运算符 表2.列名 [WHERE 条件] [ORDER BY 排序列] 其中ON 表1.列名 条件运算符 表2.列名 中,条件运算符常用的是:=、。 表1.列名和表2.列名,分别是两个表的公共列。 内连接查询出的是两

内连接

1 语法

SELECT
FROM 表1 
INNER  JOIN  表2 ON 表1.列名  条件运算符  表2.列名
[WHERE 条件]
[ORDER BY  排序列]
其中“ON 表1.列名  条件运算符  表2.列名” 中,条件运算符常用的是:=、。
表1.列名和表2.列名,分别是两个表的公共列。
内连接查询出的是两个表公共列共有的记录。

2 示例

(1)Books与Authors内连接SQL语句

<span>SELECT</span> b.BookCode <span>as</span> 图书编号, b.BookName <span>as</span> 图书名称, a.AuthorName <span>as</span><span> 作者姓名
</span><span>FROM</span> Books <span>as</span><span> b
</span><span>INNER</span> <span>JOIN</span> Authors <span>as</span> a <span>ON</span> b.AuthorID <span>=</span> a.AuthorID
Copy after login


(2)内连接另外一种写法

<span>SQL Server2005还兼容以前版本的内连接语法,即使用WHERE子句进行内连接。 
</span><span>SELECT</span> b.BookCode <span>as</span> 图书编号, b.BookName <span>as</span> 图书名称, a.AuthorName <span>as</span><span> 作者姓名
</span><span>FROM</span><span> Books b,  Authors a
</span><span>WHERE</span> b.AuthorID <span>=</span> a.AuthorID
Copy after login

3 多表内连接语法

SELECT
FROM 表1 
INNER  JOIN  表2 ON 表1.列名  条件运算符  表2.列名
INNER JOIN  表3 ON 表1.列名  条件运算符  表3.列名
……
[WHERE 条件]
[ORDER BY  排序列]

4 示例

<span>SELECT</span> b.BookCode <span>as</span> 图书编号, c.PublisherName <span>as</span><span> 出版商名,
               b.BookName </span><span>as</span> 图书名称,a.AuthorName <span>as</span><span> 作者姓名
</span><span>FROM</span>  Books b <span>INNER</span> <span>JOIN</span>  Authors a <span>ON</span> b.AuthorID<span>=</span><span>a.AuthorID 
</span><span>INNER</span> <span>JOIN</span> Publisher c <span>ON</span> b.PublisherID<span>=</span>c.PublisherID
Copy after login

外连接

左外连接语法

SELECT
FROM 左表 
LEFT  [OUTER] JOIN  右表 ON 左表.列名  条件运算符  右表.列名
[WHERE 条件]
[ORDER BY  排序列]

示例

使用左外连接查询Books与Authors表中的数据

<span>SELECT</span> b.BookCode <span>as</span> 图书编号, b.BookName <span>as</span> 图书名称, a.AuthorName <span>as</span><span> 作者姓名
</span><span>FROM</span><span> Books b 
</span><span>LEFT</span> <span>JOIN</span>  Authors a <span>ON</span> b.AuthorID<span>=</span>a.AuthorID
Copy after login

左外连接是以左表为主表,去关联右表(从表),结果集中包含主表所有数据行,如果主表的某行在从表中没有匹配行时,则从表的选择列为NULL值。

右外连接语法

SELECT
FROM 左表  RIGHT  [OUTER] JOIN  右表
ON 左表.列名  条件运算符  右表.列名
[WHERE 条件]
[ORDER BY  排序列]

示例

使用右外连接查询Books与Authors表中的数据
SELECT b.BookCode as 图书编号, b.BookName as 图书名称, a.AuthorName as 作者姓名
FROM Books b
RIGHT JOIN  Authors a ON b.AuthorID=a.AuthorID
右外连接是以右表为主表,去关联左表(从表),结果集中包含主表所有数据行,如果主表的某行在从表中没有匹配行时,则从表的选择列为NULL值。

完全连接

语法 

SELECT
FROM 左表 
FULL  [OUTER] JOIN  右表 ON 左表.列名  条件运算符  右表.列名
[WHERE 条件]
[ORDER BY  排序列]
完全连接左表和右表中所有行,当某行数据在另一个表中没有匹配时,则另一个表的选择列值为NULL

示例

使用完全连接查询Books与Authors表中的数据

<span>SELECT</span> b.BookCode <span>as</span> 图书编号, b.BookName <span>as</span> 图书名称, a.AuthorName <span>as</span><span> 作者姓名
</span><span>FROM</span><span> Books b 
</span><span>FULL</span> <span>JOIN</span>  Authors a <span>ON</span> b.AuthorID<span>=</span>a.AuthorID
Copy after login

交叉联接


概念:没有用where子句的交叉连接将产生连接所涉及的笛卡尔积第一个表的行数乘以第二个表的行数等于笛卡尔积和结果集的大小

交叉连接: Cross join(不带条件where,如果带返回或显示的是匹配的行数)

SQL语法:select * from  Books cross join Books

如果有条件(<span>where</span><span>)
</span><span>select</span> <span>*</span> <span>from</span> Books <span>cross</span> <span>join</span> Authors <span>where</span> Books.AuthorID<span>=</span><span> Authors.AuthorID
等价于
</span><span>select</span> <span>*</span> <span>from</span> Books ,Authors <span>--</span><span>(不带where)</span>
Copy after login
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
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)

12306 How to check historical ticket purchase records How to check historical ticket purchase records 12306 How to check historical ticket purchase records How to check historical ticket purchase records Mar 28, 2024 pm 03:11 PM

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

How to check your academic qualifications on Xuexin.com How to check your academic qualifications on Xuexin.com Mar 28, 2024 pm 04:31 PM

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];

How does Go language implement the addition, deletion, modification and query operations of the database? How does Go language implement the addition, deletion, modification and query operations of the database? Mar 27, 2024 pm 09:39 PM

Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

Xiaomi 15 series full codenames revealed: Dada, Haotian, Xuanyuan Xiaomi 15 series full codenames revealed: Dada, Haotian, Xuanyuan Aug 22, 2024 pm 06:47 PM

The Xiaomi Mi 15 series is expected to be officially released in October, and its full series codenames have been exposed in the foreign media MiCode code base. Among them, the flagship Xiaomi Mi 15 Ultra is codenamed "Xuanyuan" (meaning "Xuanyuan"). This name comes from the Yellow Emperor in Chinese mythology, which symbolizes nobility. Xiaomi 15 is codenamed "Dada", while Xiaomi 15Pro is named "Haotian" (meaning "Haotian"). The internal code name of Xiaomi Mi 15S Pro is "dijun", which alludes to Emperor Jun, the creator god of "The Classic of Mountains and Seas". Xiaomi 15Ultra series covers

The best time to buy Huawei Mate 60 series, new AI elimination + image upgrade, and enjoy autumn promotions The best time to buy Huawei Mate 60 series, new AI elimination + image upgrade, and enjoy autumn promotions Aug 29, 2024 pm 03:33 PM

Since the Huawei Mate60 series went on sale last year, I personally have been using the Mate60Pro as my main phone. In nearly a year, Huawei Mate60Pro has undergone multiple OTA upgrades, and the overall experience has been significantly improved, giving people a feeling of being constantly new. For example, recently, the Huawei Mate60 series has once again received a major upgrade in imaging capabilities. The first is the new AI elimination function, which can intelligently eliminate passers-by and debris and automatically fill in the blank areas; secondly, the color accuracy and telephoto clarity of the main camera have been significantly upgraded. Considering that it is the back-to-school season, Huawei Mate60 series has also launched an autumn promotion: you can enjoy a discount of up to 800 yuan when purchasing the phone, and the starting price is as low as 4,999 yuan. Commonly used and often new products with great value

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

An in-depth analysis of how HTML reads the database An in-depth analysis of how HTML reads the database Apr 09, 2024 pm 12:36 PM

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

See all articles