sql优化 a表中某字段存储b表多个id如何查询a表及b表

weixin_38988214 2018-04-10 10:17:24
举例:
a表中有 id,b_ids,title 三个字段(b_id中有多个id并以逗号隔开)
b表中有 id,name 两个字段

如何查询a.title 和 a表中的该行数据所关联的多个b.name
之前一直用
select b_ids,title from a where id = 1;
select name from b in(b_ids);
发现效率很低

曾想过如下方法
新建c表 a_id,b_id 两个字段
存储结构如下
_________
|a_id | b_id|
|1 | 2 |
|1 | 3 |
|2 | 4 |
|2 | 5 |

select title from a where id = 1;
select name from b where exists(select b_id from c where a_id=1)
希望前辈能有其他更好的解决办法(可从新设计表结构)
...全文
671 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
G.Jagger 2018-04-11
  • 打赏
  • 举报
回复
不知道你想要的展示效果 这个是分别列举的:
select a.title, b.name from a left join b on find_in_set(b.id, a.b_ids);
这个是归总的:
select a.title, group_concat(b.name) from a left join b on find_in_set(b.id, a.b_ids) group by a.id

56,682

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧