search
HomeDatabaseMysql Tutorial数据库之查询的连接方式

数据库之查询的连接方式

Jun 07, 2016 pm 03:55 PM
PrefacebeginningdatabaseWayInquireconnect

前言 在写开头的时候我想到了两个开始,比较一下 第一个开头:sql几种常见的查询连接方式 第二个开头:sql(结构化查询语言)有数据定义功能,数据查询功能,数据操作功能,数据控制功能。我们平常所说的增删改查就是sql这些功能的更加简洁的说法。现在就查

前言

在写开头的时候我想到了两个开始,比较一下

第一个开头:sql几种常见的查询连接方式
第二个开头:sql(结构化查询语言)有数据定义功能,数据查询功能,数据操作功能,数据控制功能。我们平常所说的增删改查就是sql这些功能的更加简洁的说法。现在就"查"来介绍几种常用的连接方式。
哪一种更好?我个人更加倾向于第二种。原因是第二种让我明确了我现在总结的知识在我的知识网中处于什么地方。让我的知识很有归属感。至于我为什么会想到第二种写法就要源于一趟课了。

在准备写这篇博客的过程中米老师给我们上了一堂课叫做“再谈编织知识网”。这堂课的中心思想我总结了两个方面。

第一个:学习新东西的时候要联系我们以前的东西。

第二个:全局观,"不谋万世者,不足谋一时;不谋全局者,不足谋一域。"对于我来说最大的收获不是老师讲的这两点,而是老师在讲第二点的时候无意之间提到的一句话,原话记不清出了大概的意思是:一定要有全局观,这样子才可以找到新东西里哪是你以前学习过的旧知识。这样子你的学习会轻松快乐的很多。
这句话对我有这么深的感触是因为我以前只是听老师讲全局观,但是自己对他的感觉其实是"熟悉的陌生人"。我从来不知道如何才算是有了全局观,还有全局观对我到底有何好处。但是老师说的"找到",让我一下子清楚过来了,全局观也算是刚刚开始认识了吧!现在想起来,全局观还会让我找到新知识的重点是什么。让我快速的了解他的全貌,这就为我下一步如何走奠定了基础。好处多多以后要多多题型着自己一点了。

好了现在开始进入正题,查询的几种连接方法。

连接简介

SQL提供了多种类型的连接方式,它们之间的区别在于:从相互交叠的不同数据集合中选择用于连接的行时所采用的方法不同。

我们最常用的连接方式有三种,分别是内连接,外连接;外连接又细分为左外连接,右外连接。除了这三种还有三种连接方式,分别是:全外连接,自身连接和交叉连接。

这几种连接方式的定义就简单的了解写一下吧

inner join(内连接):只返回两个表中联结字段相等的行

left outer join(左外连接):返回包括左表中的所有记录和右表中联结字段相等的记录

right outer join(右外连接):返回包括右表中的所有记录和左表中联结字段相等的记录

full outer(全外连接):返回左表和右表中全部的记录

自身连接:同一个表自己与自己连接。

cross join(交叉连接):交叉连接返回左表中的所有行,左表中的每一行与右表中的所有行组合

光看这些定义可以会有一种讲的都是什么的感觉,下面就用一种图形将它们简单的介绍一下,然后再通过具体的实例进行测试。

下图中一个圆圈代表一个表,其中蓝色的代表左表,白色的代表右表

\

从图上再来看几种连接就会清楚很多。

内连接:就是图中的C部分

左外连接:就是图中的A+C部分

右外联结:就是图中的B+C部分

实例

建立两种表分别是Table_a Table_b,在表中添加几条记录;如下图

\

\

1. 内连接

--内连接(inner) 可以省略inner
select a_UserID,b_StudentID from Table_a
inner join Table_b on a_UserID = b_StudentID  
结果 

\

 

2. 外连接之左外连接

--外连接之左连接 也可以写成left join(省略outer)
select a_UserID,b_StudentID from Table_a
left outer join Table_b on a_UserID = b_StudentID 

结果

\

 

3.外连接之右外连接

--外连接之右连接  也可以写成 right join(省略outer)
select  a_UserID,b_StudentID  from Table_a
right outer join Table_b on a_UserID = b_StudentID

结果 

\

 

4.全外连接

--全外连接
select a_UserID , b_StudentID from Table_a
full outer join Table_b on a_UserID=b_StudentID
结果

 

\

 

5.自身连接

--自身连接
select s.a_ID , s.a_UserName, i.a_UserID, i.a_UserName from Table_a i
join Table_a s on i.a_UserID = s.a_ID
结果

 

\

 

6.交叉连接

--交叉连接
select i.a_UserID,t.b_StudentID from Table_a i cross join Table_b t
select count(*) from Table_a i cross join Table_b t

结果

\

 

总结

这篇文章介绍常用的查询方法的使用方法,通过图形将它的定义图形化,使得理解起来更加的简单。

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

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)