Mysql:由逗号分隔的id组成的varchar联表替换成对应文字
有两张表,用户表和标签表:
create table eb_user
(id int unsigned auto_increment comment '用户id'primary key,name varchar(25) default '' not null comment '姓名',birthday varchar(32) default '' not null comment '生日',tag_id varchar(255) default '' null comment '标签id,英文逗号分隔'
)comment '用户表';
create table eb_user_tag
(id smallint unsigned auto_incrementprimary key,name varchar(64) null comment '标签名称'
)comment '用户标签表';
现在要查询用户表,用户表的tag_id字段存的是标签表的主键,我希望一条SQL查询用户的时候,直接把tag_id替换成对应的标签名字:
select mu.id as userId,mu.name as name,mu.tag_id as tagIds,(select group_concat(eut.name order by eut.id ASC separator ',')from eb_user eu1JOIN eb_user_tag eut on find_in_set(eut.id, mu.tag_id)where eu1.id = mu.id) as tagName
from eb_user mu
这样子,查询出来的tagName就是tagIds对应的标签名字,也是用逗号分隔