Mysql join query principle knowledge points

Mysql join query principle knowledge points

Mysql join query

1. Basic concepts

Connect each row of the two tables horizontally in pairs to obtain the results of all rows.

Assumptions:

Table A has n1 rows and m1 columns;

Table B has n2 rows and m2 columns;

After Table A and Table B are "connected", we will have:

n1*n2 rows;

m1+m2 columns.

2. The result after they are connected is similar to this:

3. Basic form of join query: from table 1 [join method] join table 2 [on join condition] Basic form of join query: from table 1 [join method] join table 2 [on join condition]

1. Classification of connection queries

Cross Connect

In fact, it is the "all data" obtained after connecting two tables according to the basic concept of connection, without any "filtering" result - filtering refers to the connection condition.

That is, a cross join is an unconditional "join all" - also known as a Cartesian product.

Cross joins usually have no practical value because after joining the data, the meaning of each row of data may be "lost".

form:

from table1 [cross] join table2;

or:

from Table1 , Table2 ;

Inner Join

form:

from table1 [inner] join table2 on table1.field1 = table2.field2;

meaning:

Get the data of the rows that meet the set connection conditions (the conditions after on) in the result of a "cross connection";

Cross joins often have "meaningless data", as follows:

2. Look at the results of the inner connection:

3. The result is:

4. It can be seen that inner join is actually to find the "meaningful" data rows in the data results of a cross join. In a cross join, some of the data are meaningful, while some are meaningless (erroneous data).

However, please note:

  • 1. This connection condition is not set arbitrarily, but must be set according to the actual relationship between the tables. Usually, the relationship is that the values ​​of the two fields with a "primary and foreign key relationship" between the two tables are equal.
  • 2. It can be seen that the connection query has its inherent logical consistency with the "foreign key relationship" we learned before.
  • 3. However, when we do an inner join, it does not require that the two tables "must" have a foreign key relationship - we just understand from a practical perspective that they have a foreign key relationship (data relationship), and when using an inner join during querying, their relationship is established. It can be seen that inner join is actually to find the "meaningful" data rows in the data results of a cross join. In a cross join, some of the data are meaningful, while some are meaningless (erroneous data).

You may also be interested in:
  • In-depth understanding of MySQL self-connection and join association
  • Analysis of MySQL multiple left join query usage
  • MySQL optimization: use join instead of subquery
  • MySQL query optimization: Introduction to join query sort limit (join, order by, limit statement)
  • Detailed tutorial on using JOIN statement to perform connection operations in MySQL
  • Mysql join query syntax and examples
  • Summary of several commonly used join connection methods in Mysql

<<:  CentOS7 installation GUI interface and remote connection implementation

>>:  JavaScript canvas to achieve raindrop effect

Recommend

What you need to know about MySQL auto-increment ID

Introduction: When using MySQL to create a table,...

MySQL pessimistic locking and optimistic locking implementation

Table of contents Preface Actual Combat 1. No loc...

Detailed explanation of long transaction examples in MySQL

Preface: The "Getting Started with MySQL&quo...

Front-end AI cutting tips (experience)

AI image cutting needs to be coordinated with PS....

A Different Kind of "Cancel" Button

The “Cancel” button is not part of the necessary ...

A general method for implementing infinite text carousel with native CSS

Text carousels are very common in our daily life....

The images in HTML are directly replaced by base64 encoded strings

Recently, I came across a webpage that had images ...

Solution to forgetting mysql database password

You may have set a MySQL password just now, but f...

SpringBoot integrates Activiti7 implementation code

After the official release of Activiti7, it has f...

How to deploy gitlab using Docker-compose

Docker-compose deploys gitlab 1. Install Docker I...

How to use the vue timeline component

This article example shares the specific implemen...

How to try to add sticky effect to your CSS

Written in front I don’t know who first discovere...

This article takes you into the world of js data types and data structures

Table of contents 1. What is dynamic typing? 2. D...

Detailed explanation of using JavaScript WeakMap

A WeakMap object is a collection of key/value pai...