首先把 图二releated_id拆分成多行。
然后再关联查询。
mysql 事例
-- 列转多行
-- drop table if exists test;
-- create table test(id int,ids varchar(50));
-- insert into test
-- select 1,'1,12,135' union all
-- select 2,'2,25,234';
with split(id,splid,idsstr) as
(
select id,'',ids||',' from test
UNION ALL
SELECT id,substr(idsstr, 0, instr(idsstr, ',')),substr(idsstr, instr(idsstr, ',')+1)
FROM split WHERE idsstr!=''
)
select id,splid from split
where splid != ''
order by id ;