Detailed explanation of Mysql self-join query example

Detailed explanation of Mysql self-join query example

This article describes the Mysql self-join query. Share with you for your reference, the details are as follows:

Self-join query

Imagine the following scenario: an e-commerce website wants to classify the products on the site into a hierarchy, with one category having several subcategories, and each subcategory having other subcategories. For example, under the category of digital products, there are laptops, desktops, smartphones, etc.; laptops, desktops, and smartphones can be classified by brand; brands can be classified by price, and so on. Perhaps these classifications will reach a very deep level and present a tree-like structure. So how should this data be represented in the database? We can create two fields in the database to store the id and category name, use a third field to store the id of the subcategory or parent category of the category, and finally query the desired results through self-join.

A self-join query is actually equivalent to a join query, which requires two tables, except that both its left table (parent table) and right table (child table) are itself. When doing a self-join query, you join the table to itself, give the parent table and the child table two different aliases, and then attach the join conditions. Take a look at the following example:

1. Create a data table:

create table tdb_cates(
 id smallint primary key auto_increment,
 cate_name varchar(20) not null,
 parent_id smallint not null
);

Note: cate_name represents the name of the category, and parent_id represents the id of the parent category.

2. Insert data:

insert into tdb_cates(cate_name, parent_id) values('digital products', 0);
insert into tdb_cates(cate_name, parent_id) values('Household Products', 0);
insert into tdb_cates(cate_name, parent_id) values('Notebook', 1);
insert into tdb_cates(cate_name, parent_id) values('smartphone', 1);
insert into tdb_cates(cate_name, parent_id) values('Electrical appliances', 2);
insert into tdb_cates(cate_name, parent_id) values('furniture', 2);
insert into tdb_cates(cate_name, parent_id) values('Refrigerator', 5);
insert into tdb_cates(cate_name, parent_id) values('washing machine', 5);
insert into tdb_cates(cate_name, parent_id) values('Car brand', 0);
insert into tdb_cates(cate_name, parent_id) values('Buick', 9);
insert into tdb_cates(cate_name, parent_id) values('宝马', 9);
insert into tdb_cates(cate_name, parent_id) values('Chevrolet', 9);
insert into tdb_cates(cate_name, parent_id) values('Home Textiles', 0);

Query results:

3. Query all categories and their parent categories: Assume that there are two tables (both are tdb_cates), the left table is the child table, and the right table is the parent table; query the id of the child table, the cate_name of the child table, and the cate_name of the parent table; the join condition is that the parent_id of the child table is equal to the id of the parent table.

Copy the code as follows:
select s.id, s.cate_name, p.cate_name from tdb_cates s left join tdb_cates p on s.parent_id=p.id;

Query results:

4. Query all categories and subcategories of categories: Let’s still assume that there are two tables (both are tdb_cates), the left table is the child table, and the right table is the parent table; query the id of the child table, the cate_name of the child table, and the cate_name of the parent table; the connection condition is that the id of the child table is equal to the parent_id of the parent table.

Copy the code as follows:
select s.id, s.cate_name, p.cate_name from tdb_cates s left join tdb_cates p on p.parent_id=s.id;

Query results:

Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL query skills", "MySQL common functions summary", "MySQL log operation skills", "MySQL transaction operation skills summary", "MySQL stored procedure skills" and "MySQL database lock related skills summary"

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • Problems with join queries and subqueries in MySQL
  • Detailed explanation of MySQL multi-table join query
  • What kinds of MYSQL connection queries do you know?
  • The principle and application of MySQL connection query
  • Mysql join query syntax and examples
  • Detailed explanation of the principles and usage examples of MySQL join query, union query, and subquery
  • MySQL multi-table join query example explanation
  • Detailed explanation of mysql connection query

<<:  Vue implements login jump

>>:  Tutorial on installing Apache 2.4.41 on Windows 10

Recommend

Vue implements a movable floating button

This article example shares the specific code of ...

How many ports can a Linux server open at most?

Table of contents Port-related concepts: Relation...

Example code for mixing float and margin in CSS

In my recent studies, I found some layout exercis...

Teach you how to build hive3.1.2 on Tencent Cloud

Environment Preparation Before starting any opera...

Tutorial on how to modify element.style inline styles

Preface When we were writing the web page style a...

Why MySQL should avoid large transactions and how to solve them

What is a big deal? Transactions that run for a l...

mysql IS NULL using index case explanation

Introduction The use of is null, is not null, and...

A detailed introduction to for/of, for/in in JavaScript

Table of contents In JavaScript , there are sever...

Linux kernel device driver memory management notes

/********************** * Linux memory management...

Two ways to remove the 30-second ad code from Youku video

I believe everyone has had this feeling: watching ...

How to hide the version number in Nginx

Nginx hides version number In a production enviro...

A comprehensive summary of frequently used statements in MySQL (must read)

The knowledge points summarized below are all fre...