MySQL 表中一个字段关联多个code,查询出对应的层级名称


使用步骤

1.用到MySQL的FIND_IN_SET()、group_concat()函数,请自行查阅API,这里只解决问题。

业务表存储的数据:

在这里插入图片描述

2.两种实现方式sql

第一种:

select
T1.SUBJECT_CODE ,
group_concat(T2.SUBJECT_NAME order by T2.SUBJECT_CODE separator ’ / ') as SUBEJCT_NAME
from
t_itac_subject T1 ,
t_itac_subject T2
where
T1.DATA_STATUS = 1
and T2.DATA_STATUS = 1
and FIND_IN_SET(T2.SUBJECT_CODE, T1.SUBJECT_LEVEL)
group by
T1.SUBJECT_CODE ;

第二种:

select
t.SUBJECT_LEVEL,
t.SUBJECT_CODE,
(
select
replace(GROUP_CONCAT(s1.SUBJECT_NAME),","," / ")
from
t_itac_subject s1
where
s1.SUBJECT_CODE in
(
select substring_index(substring_index(a.SUBJECT_LEVEL,’,’,b.help_topic_id+1),’,’,-1) as code
from
t_itac_subject a
join
mysql.help_topic b
on b.help_topic_id < (length(a.SUBJECT_LEVEL) - length(replace(a.SUBJECT_LEVEL,’,’,’’))+1)
where SUBJECT_CODE = t.SUBJECT_CODE
)
) as identitycategoriesname
from
t_itac_subject t
where
t.SUBJECT_CODE = ‘10200601’
and t.DATA_STATUS = 1;


效果图

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值