如何查询一个表的一个单元格对应另一张表一个字段的多个单元格

ab73790185cb0c0d50c5ceffb234199.png

FineReport 李豆芽 发布于 2023-5-11 10:44 (编辑于 2023-5-11 10:46)
1min目标场景问卷 立即参与
回答问题
悬赏:0 F币 + 添加悬赏
提示:增加悬赏、完善问题、追问等操作,可使您的问题被置顶,并向所有关注者发送通知
共1回答
最佳回答
0
青春只因年少Lv6中级互助
发布于2023-5-11 10:51(编辑于 2023-5-11 10:52)

首先把 图二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 ;

  • 2关注人数
  • 278浏览人数
  • 最后回答于:2023-5-11 10:52
    请选择关闭问题的原因
    确定 取消
    返回顶部