经典sql语句
1.查询三级分类展示数据
表结构:
CREATE TABLE `t_base_product_category` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
`category_type` int(1) NOT NULL DEFAULT '1' COMMENT '分类类型 1:基础分类;2:运营分类',
`category_level` int(1) NOT NULL DEFAULT '1' COMMENT '分类级别',
`category_code` varchar(50) DEFAULT NULL COMMENT '分类code',
`category_name` varchar(50) DEFAULT NULL COMMENT '商品分类名称',
`parent_id` bigint(20) NOT NULL COMMENT '父分类id',
`category_sort` int(3) DEFAULT '0' COMMENT '分类排序值 默认为0',
`category_image_url` varchar(500) DEFAULT NULL COMMENT '分类主图地址',
`is_delete` tinyint(1) DEFAULT '0' COMMENT '是否被删除 0 否 1 是',
`create_by` bigint(20) NOT NULL COMMENT '创建人',
`create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`last_update_by` bigint(20) DEFAULT NULL COMMENT '最后更新人',
`last_update_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '最后修改时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='商品运营分类表';
sql语句
SELECT t1.id,t2.id,t3.id,t3.category_name 一级,t2.category_name 二级,t1.category_name 三级 FROM t_base_product_category t1
LEFT JOIN t_base_product_category t2 ON t1.parent_id = t2.id
LEFT JOIN t_base_product_category t3 ON t2.parent_id=t3.id
where t2.id is not null and t3.id is not null
ORDER BY t3.category_name,t2.category_name,t1.category_name
效果图: