search
HomeDatabaseMysql TutorialSQL 连接查询语法及使用

一、交叉连接(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指定的列返回查询结果。

Statement
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
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

See all articles

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

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.