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

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)

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

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

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.

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

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

What are the syntax and structure characteristics of lambda expressions? What are the syntax and structure characteristics of lambda expressions? Apr 25, 2024 pm 01:12 PM

Lambda expression is an anonymous function without a name, and its syntax is: (parameter_list)->expression. They feature anonymity, diversity, currying, and closure. In practical applications, Lambda expressions can be used to define functions concisely, such as the summation function sum_lambda=lambdax,y:x+y, and apply the map() function to the list to perform the summation operation.

How to connect to remote database using Golang? How to connect to remote database using Golang? Jun 01, 2024 pm 08:31 PM

Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.

See all articles