MySQL multi-table join introductory tutorial

MySQL multi-table join introductory tutorial

Connections can be used to query, update, and establish factual foreign keys (referring to the correspondence between two tables created artificially. In contrast, FORGIEN KEY is also called physical foreign key)

The essence of table connection is the inverse constraint of foreign key

Connection conditions

Use ON to set the connection condition, or you can use WHERE instead.

In general

  • ON: Set the connection condition
  • WHERE: Filter the result set records

The essence of an unconditional JOIN inner join is a Cartesian product.

[INNER] JOIN inner join

In MySQL, JOIN, CROSS JOIN and INNER JOIN are equivalent.

Inner join means intersection, and only records in tables A and B that meet the join conditions are displayed. Records that do not meet the connection conditions are not displayed.

SELECT goods_id,goods_name,cate_name 
FROM tdb_goods 
INNER JOIN tdb_goods_cate 
ON tdb_goods.cate_id = tdb_goods_cate.cate_id;

Use inner join to update multiple tables:

--Change the goods_cate stored in Chinese in the tdb_goods table to the corresponding cate_id in the tdb_goods_cates table to save space UPDATE tdb_goods 
INNER JOIN tdb_goods_cates
ON goods_cate=cate_name 
SET goods_cate=cate_id;
--tdb_goods The name of the table you want to change --tdb_goods_cates The associated appendix --goods_cate=cate_name The relationship between the corresponding columns of the two tables --goods_cate=cate_id; Set value

Outer Join

Inner joins are used more often than outer joins.

If a field exists only in one table, the field in the other table returns NULL

LEFT [OUTER] JOIN Left outer join

Display all records in the left table and records in the right table that meet the connection conditions.

  • If LEFT JOIN is used, and there is a record A in the left table, but no corresponding record is found in the right table, then the returned result will contain only the corresponding field content in record A, and all other fields are NULL (similar to RIGHT JOIN).

RIGHT [OUTER] JOIN Right outer join

Display all records in the right table and records in the left table that meet the connection conditions.

Multi-table join

Similar to joining two tables

Such as the connection of three tables:

SELECT goods_id,goods_name,b.cate_name,c.brand_name,goods_price
FROM products AS a 
INNER JOIN products_cate AS b 
ON a.goods_cate = b.cate_id
INNER JOIN products_brand AS c 
ON a.brand_name = c.brand_id;

Self-join

Design unlimited classification data table

There are both parent classes and child classes in the same table, which is essentially a tree:

You can query the same data table by connecting to it:

--Find the name corresponding to the parent class id SELECT s.type_id,s.type_name,p.type_name AS parent_name
FROM tdb_goods_types AS s 
LEFT JOIN tdb_goods_types AS p 
ON s.parent_id=p.type_id;

--Find the name corresponding to the subclass id SELECT p.type_id,p.type_name,s.type_name AS child_name
FROM tdb_goods_types AS p
LEFT JOIN tdb_goods_types AS s
ON p.type_id=s.parent_id;

--Find out how many children there are SELECT p.type_id,p.type_name,COUNT(s.type_name) AS child_count
FROM tdb_goods_types AS p
LEFT JOIN tdb_goods_types AS s
ON p.type_id=s.parent_id 
GROUP BY p.type_name
ORDER BY p.type_id;

Multiple table query and delete

Here, we use a self-join to simulate two tables, delete duplicate items in the table, and keep records with smaller goods_id.

DELETE t1 
FROM tdb_goods AS t1 
LEFT JOIN(--Select duplicate records of goods_nameSELECT goods_id,goods_name 
 FROM tdb_goods 
 GROUP BY goods_name --MySQL 5.7.5 and above versions enable the only_full_group_by SQL mode. The selected columns must be in the group or be aggregate columns (SUM, AVG, MAX, MIN). HAVING COUNT(goods_name)>=2 is not enabled here. 
AS t2 --Left join t1 and t2. In fact, inner join and right join are also possible. ON t1.goods_name=t2.goods_name --Join condition of t1 and t2 WHERE t1.goods_id>t2.goods_id; --Select the records that satisfy t1.goods_id>t2.goods_id in the LEFT JOIN result set

To help understand, the result of LEFT JOIN is:

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Comparison of efficiency between single-table query and multi-table join query in MySql database
  • Detailed explanation of MySQL multi-table join query
  • A brief discussion on the execution details of Mysql multi-table join query
  • MySQL multi-table join query example explanation
  • mysql three tables connected to create a view
  • A simple tutorial on optimizing table join queries in MySQL
  • Basic multi-table join query tutorial in MySQL
  • MySQL and PHP basics and application topics: table connection

<<:  js+css to realize three-level navigation menu

>>:  A brief introduction to Linux environment variable files

Recommend

Ubuntu 20.04 Best Configuration Guide (Newbie Essential)

1. System Configuration 1. Turn off sudo password...

Solution to data duplication when using limit+order by in MySql paging

Table of contents summary Problem Description Ana...

vue-element-admin global loading waiting

Recent requirements: Global loading, all interfac...

Detailed installation process of MySQL5.6.40 under CentOS7 64

MySQL5.6.40 installation process under CentOS7 64...

How to debug loader plugin in webpack project

Recently, when I was learning how to use webpack,...

Vue custom optional time calendar component

This article example shares the specific code of ...

Some parameter descriptions of text input boxes in web design

<br />In general guestbooks, forums and othe...

Detailed tutorial on installing Ubuntu 19.10 on Raspberry Pi 4

Because some dependencies of opencv could not be ...

How to smoothly go online after MySQL table partitioning

Table of contents Purpose of the table For exampl...

How to use VirtualBox to build a local virtual machine environment on Mac

1. Big Data and Hadoop To study and learn about b...

How to use union all in MySQL to get the union sort

Sometimes in a project, due to some irreversible ...

Detailed explanation of the update command for software (library) under Linux

When installing packages on an Ubuntu server, you...

Detailed explanation of how to create an updateable view in MySQL

This article uses an example to describe how to c...

Summary of ten principles for optimizing basic statements in MySQL

Preface In the application of database, programme...