MySQL connection query super detailed explanation
1 Function
In the database, the join
operation is called a connection, and its function is to connect multiple The data of each table (through connection conditions), the data obtained from multiple tables are merged together and returned to the client as a result set. For example:
Table A:
name | age | |
---|---|---|
A | 18 | ## 2 |
19 | 3 | |
20 |
gender | ##1 | |
---|---|---|
2 | 2 | |
The data of the merged two tables can be obtained through connection: |
id
gender | 1 | ||
---|---|---|---|
F | ##2 | B | |
M | 3 | C | |
null | 2 连接关键字连接两个表我们可以用两个关键字: on: select * from A join B on A.id=B.id and B.name='' using: select * from A join B using(id,name) = select * from A join B on A.id=B.id and A.name=B.name Copy after login 3 连接类型3.1 内连接内连接和交叉连接
隐式连接
3.2 外连接左外连接
右外连接
全外连接MySQL不支持全外连接,只支持左外连接和右外连接。如果要获取全连接的数据,要可以通过合并左右外连接的数据获取到,如 这里 3.3 自然连接
4 执行顺序在连接过程中,MySQL各关键字执行的顺序如下: from -> on|using -> where -> group by -> having -> select -> order by -> limit Copy after login 可以看到,连接的条件是先于 5 连接算法
具体来说
我们来看下对于连接语句 5.1 Simple Nested Loop Join(SNLJ)
for (a in A) { for (b in B) { if (a.id == b.tid) { output <a, b>; } } } Copy after login 当然,MySQL即使在无索引可用,或者判断全表扫描可能比使用索引更快的情况下,还是不会选择使用过于粗暴的 5.2 Block Nested Loop Join(BNLJ)
for (blockA in A.blocks) { for (b in B) { if (b.tid in blockA.id) { output <a, b>; } } } Copy after login 相比于 原理 举例来说,外层循环的结果集是100行,使用 当然这里,不管
影响因素 这里 可以看出,在这个式子里, 那什么会影响 5.3 Index Nested Loop Join(INLJ)INLJ是MySQL判断能使用到被驱动表的索引的情况下采用的算法。假设 for (a in A) { if (a.id in B.tid.Index) { output <a, tid.Index所在行>; } } Copy after login 总共需要循环10次 INLJ内层循环读取的是索引,可以减少内存循环的次数,提高 6 Notes
7 Frequently Asked Questions about Outer JoinsQ: If you want to filter the data in the driver table, for example, a left join filters the data in the left table, should you filter in the join condition or Q: What should I do if the data rows matched by the driven table are not unique and the final connection data exceeds the data volume of the driving table? For example, for a left join, the matching data rows in the right table are not unique.
|
The above is the detailed content of MySQL connection query super detailed explanation. For more information, please follow other related articles on the PHP Chinese website!

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

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.
