基于逗号分隔值连接表 [英] Joining a table based on comma separated values

查看:35
本文介绍了基于逗号分隔值连接表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何连接两个表,其中一个表的一列中有多个逗号分隔值引用另一列中的 id?

How can I join two tables, where one of the tables has multiple comma separated values in one column that reference an id in another column?

第一张桌子

Name    | Course Id
====================
Zishan  | 1,2,3                                           
Ellen   | 2,3,4                

第二张桌子

course id | course name 
=======================
   1      |  java
   2      |  C++
   3      |  oracle
   4      |  dot net

推荐答案

可能是这个丑,我还没查结果:

Maybe this uglyness, I have not checked results:

select names.name, courses.course_name
from names inner join courses
    on ',' + names.course_ids + ',' like '%,' + cast(courses.course_id as nvarchar(20)) + ',%'

这篇关于基于逗号分隔值连接表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆