SELECT
t.id,
CASE
WHEN t.parent_id <> 1 THEN
concat('└─', t.item_name)
ELSE
t.item_name
END item_name,
concat(t.parent_id, t.id) AS sorts
FROM
tb_item t,
tb_item t1
WHERE
t.id <> 1
AND t.parent_id = t1.id
ORDER BY
concat(t.parent_id, t.id)
先贴代码
表结构如下
CREATE TABLE `tb_item` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`item_name` varchar(200) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '名称',
`sub_title` varchar(2000) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '副标题',
`parent_id` bigint(20) DEFAULT '0' COMMENT '父栏目',
`art_id` bigint(20) DEFAULT NULL COMMENT '',
`sort` int(4) DEFAULT '0',
`content` text COLLATE utf8mb4_unicode_ci COMMENT '内容',
PRIMARY KEY (`id`)
)