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

In-depth understanding of CSS @font-face performance optimization

This article mainly introduces common strategies ...

Tutorial on building svn server with docker

SVN is the abbreviation of subversion, an open so...

15-minute parallel artifact GNU Parallel Getting Started Guide

GNU Parallel is a shell tool for executing comput...

What are Web Slices?

IE8 new feature Web Slices (Web Slices) Microsoft...

How to build mysql master-slave server on centos7 (graphic tutorial)

This article mainly introduces how to build a MyS...

Detailed explanation of prototypes and prototype chains in JavaScript

Table of contents Prototype chain diagram Essenti...

Detailed explanation of the role of key in React

Table of contents Question: When the button is cl...

New usage of watch and watchEffect in Vue 3

Table of contents 1. New usage of watch 1.1. Watc...

Solution for importing more data from MySQL into Hive

Original derivative command: bin/sqoop import -co...

Detailed explanation of CocosCreator message distribution mechanism

Overview This article begins to introduce content...

Summary of the understanding of virtual DOM in Vue

It is essentially a common js object used to desc...

Detailed explanation of webpage screenshot function in Vue

Recently, there is a requirement for uploading pi...