当前位置:网站首页>Mysql自连接查询「建议收藏」
Mysql自连接查询「建议收藏」
2022-06-28 15:50:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
自连接查询
假想以下场景:某一电商网站想要对站内产品做层级分类,一个类别下面有若干子类,子类下面也会有别的子类。例如数码产品这个类别下面有笔记本,台式机,智能手机等;笔记本,台式机,智能手机又可以按照品牌分类;品牌又可以按照价格分类,等等。也许这些分类会达到一个很深的层次,呈现一种树状的结构。那么这些数据要怎么在数据库中表示呢?我们可以在数据库中创建两个字段来存储id和类别名称,使用第三个字段存储类别的子类或者父类的id,最后通过自连接去查询想要的结果。
自连接查询其实等同于连接查询,需要两张表,只不过它的左表(父表)和右表(子表)都是自己。做自连接查询的时候,是自己和自己连接,分别给父表和子表取两个不同的别名,然后附上连接条件。看下面的例子:
1. 创建数据表:
create table tdb_cates(
id smallint primary key auto_increment,
cate_name varchar(20) not null,
parent_id smallint not null
);
注:cate_name表示分类的名称,parent_id表示父类的id。
2. 插入数据:
insert into tdb_cates(cate_name, parent_id) values('数码产品', 0);
insert into tdb_cates(cate_name, parent_id) values('家用产品', 0);
insert into tdb_cates(cate_name, parent_id) values('笔记本', 1);
insert into tdb_cates(cate_name, parent_id) values('智能手机', 1);
insert into tdb_cates(cate_name, parent_id) values('电器', 2);
insert into tdb_cates(cate_name, parent_id) values('家具', 2);
insert into tdb_cates(cate_name, parent_id) values('冰箱', 5);
insert into tdb_cates(cate_name, parent_id) values('洗衣机', 5);
insert into tdb_cates(cate_name, parent_id) values('汽车品牌', 0);
insert into tdb_cates(cate_name, parent_id) values('别克', 9);
insert into tdb_cates(cate_name, parent_id) values('宝马', 9);
insert into tdb_cates(cate_name, parent_id) values('雪佛兰', 9);
insert into tdb_cates(cate_name, parent_id) values('家纺', 0);
查询结果:
3. 查询所有分类以及分类的父类:假想有左右两张表(都是tdb_cates),左表是子表,右表是父表;查询子表的id,子表的cate_name,父表的cate_name;连接条件是子表的parent_id等于父表的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;
查询结果:
4. 查询所有分类以及分类的子类:还是假想有左右两张表(都是tdb_cates),左表是子表,右表是父表;查询子表的id,子表的cate_name,父表的cate_name;连接条件是子表的id等于父表的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;
查询结果:
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/132874.html原文链接:https://javaforall.cn
边栏推荐
- Navicat 15 for MySQL
- Geoffrey Hinton:我的五十年深度学习生涯与研究心法
- Summary of language features of fluent dart
- Privacy computing fat - offline prediction
- Change exchange (dynamic planning)
- 一个bug肝一周...忍不住提了issue
- A new 25K byte from the Department showed me what the ceiling is
- 全球陆续拥抱Web3.0,多国已明确开始抢占先机
- Web3.0时代来了,看天翼云存储资源盘活系统如何赋能新基建(上)
- Technical secrets of ByteDance data platform: implementation and optimization of complex query based on Clickhouse
猜你喜欢
Qt create 5.0.3 配置Qt4.8.7
看界面控件DevExpress WinForms如何创建一个虚拟键盘
薅羊毛的机会了,点个“赚”即有机会赚取高额佣金
Realization of a springboard machine
Lecturer solicitation order | Apache dolphin scheduler meetup sharing guests, looking forward to your topic and voice!
See how the interface control devaxpress WinForms creates a virtual keyboard
Today's sleep quality record is 80 points
What useful supplier management systems are available
Visual Studio 2010 configuring and using qt5.6.3
Coding Devops helps Sinochem information to build a new generation of research efficiency platform and drive the new future of "online Sinochem"
随机推荐
ROS knowledge points - build an ROS development environment using vscode
超自动化与网络安全的未来
openGauss内核:SQL解析过程分析
Expand Disk C (allocate the memory of disk d to Disk C)
What are the most powerful small and medium-sized companies in Beijing?
Basic grammar of C language
数组中的第K大元素[堆排 + 建堆的实际时间复杂度]
成功迁移到云端需要采取的步骤
【高并发基础】MySQL 不同事务隔离级别下的并发隐患及解决方案
Flutter simply implements multilingual internationalization
OpenHarmony—内核对象事件之源码详解
RedmiBook Pro 14增强版 打不开台达软件DRAStudio_v1.00.07.52
【MySQL】官网文档学习之查询语句sql注意事项
Navicat 15 for MySQL
机器学习之深度学习简介
如何查询数据库中一个表中的所有数据呢?
wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe-Module
MIPS assembly language learning-01-sum of two numbers, environment configuration and how to run
一种跳板机的实现思路
扩充C盘(将D盘的内存分给C盘)