当前位置:网站首页>MySQL self connection query "suggestions collection"
MySQL self connection query "suggestions collection"
2022-06-28 16:11:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
Self connect query
Imagine the following scenario : An e-commerce website wants to make hierarchical classification of products in the station , There are several subclasses under a category , There will also be other subclasses under subclasses . For example, there are notebooks under the category of digital products , Desktop computer , Smart phones and so on ; The notebook , Desktop computer , Smart phones can be classified by brand ; Brands can be classified according to price , wait . Perhaps these categories will reach a very deep level , Present a tree like structure . So how can these data be represented in the database ? We can create two fields in the database to store id And category name , Use the third field to store the contents of the subclass or parent of the category id, Finally, query the desired results through self connection .
Self join query is actually equivalent to join query , I need two tables , Just its left watch ( Parent table ) And the right watch ( Sub table ) They are all themselves . When doing self connection query , It's about connecting with yourself , Give the parent table and the child table two different aliases , Then attach the connection conditions . See the following example :
1. Create data table :
create table tdb_cates(
id smallint primary key auto_increment,
cate_name varchar(20) not null,
parent_id smallint not null
);notes :cate_name Indicates the name of the classification ,parent_id Represents the parent class id.
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(' The notebook ', 1);
insert into tdb_cates(cate_name, parent_id) values(' A smart phone ', 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(' The 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(' BMW ', 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 classes : Suppose there are two tables on the left and right ( All are tdb_cates), The left table is a child table , The right table is the parent table ; Query sub table id, Of child tables cate_name, The parent table of the cate_name; The join condition is a child table parent_id Equal to of parent table id.
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 their subclasses : Or suppose there are two tables ( All are tdb_cates), The left table is a child table , The right table is the parent table ; Query sub table id, Of child tables cate_name, The parent table of the cate_name; The join condition is a child table id Equal to of parent table parent_id.
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 :
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/132874.html Link to the original text :https://javaforall.cn
边栏推荐
- Practice of curve replacing CEPH in Netease cloud music
- 3. caller service call - dapr
- What is the difference between treasury bonds and time deposits
- Soliciting articles and contributions - building a blog environment with a lightweight application server
- Application of mongodb in Tencent retail premium code
- Android, eclipse and MySQL upload pictures and get
- 防火墙基础之流量管理与控制
- How can the sports app keep the end-to-side background alive to make the sports record more complete?
- Focus on the 35 year old Kan: fear is because you don't have the ability to match your age
- Summary of language features of fluent dart
猜你喜欢

【MySQL】官网文档学习之查询语句sql注意事项

What are the most powerful small and medium-sized companies in Beijing?

The world has embraced Web3.0 one after another, and many countries have clearly begun to seize the initiative

【Proteus仿真】L297驱动步进电机

Basic grammar of C language

Qt5.5.1 configuring msvc2010 compiler and WinDbg debugger
![[high concurrency foundation] hidden dangers and solutions of MySQL concurrency under different transaction isolation levels](/img/35/63c9793ec7bc1c90c759504e84dc96.png)
[high concurrency foundation] hidden dangers and solutions of MySQL concurrency under different transaction isolation levels

Lecturer solicitation order | Apache dolphin scheduler meetup sharing guests, looking forward to your topic and voice!

北京有哪些牛逼的中小型公司?

What useful supplier management systems are available
随机推荐
Mysql自连接查询「建议收藏」
国债与定期存款哪个更安全 两者之间有何区别
数字藏品热潮之下,你必须知道的那些事儿
成功迁移到云端需要采取的步骤
3. caller service call - dapr
早晨有些犹豫
Privacy computing fat - offline prediction
抖音实战~我关注的博主列表、关注、取关
北京有哪些牛逼的中小型公司?
知道这几个命令让你掌握Shell自带工具
使用Karmada实现Helm应用的跨集群部署
Deep learning convolutional neural network of machine learning to realize handwritten font recognition based on CNN network
10年测试经验,在35岁的生理年龄面前,一文不值
[high concurrency foundation] MySQL index optimization
NFT质押LP流动性挖矿系统开发详情
机器学习之卷积神经网络使用cifar10数据集和alexnet网络模型训练分类模型,安装labelimg,以及报错ERROR
How can the sports app keep the end-to-side background alive to make the sports record more complete?
【高并发基础】MySQL索引优化
深度学习基础汇总
机器学习之卷积神经网络Lenet5训练模型