SQL查询在1 db列中使用逗号分隔多个值以连接另一个表,并在以逗号分隔的新列中获取值。 [英] SQL query to have multiple values seperated by comma in 1 db column to join another table and get the values in a new column seperated by comma.

查看:76
本文介绍了SQL查询在1 db列中使用逗号分隔多个值以连接另一个表,并在以逗号分隔的新列中获取值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个表A,其列中有一些代码用逗号在列数据中编写,如下所示



Centercodenumbers

1,2,3,4





另一个表B列和值



CenterCode CenterName

1 ---------------印度

2 ---------------- US

3 ----------------加拿大

4 ---------------- ..

5 ------------- ---- ..

.. ---------------- ..

50 ------- --------- IreLand



现在我想加入这两个表并获得另一个新列,其中包含如下数据



NewCenterValues

印度,美国,加拿大



这个代码值高于

Centercodenumbers

1,2,3,4







可以吗nyone请帮助..





我用C#在前端使用SPLIT显示上述值(',' )



但是在这个要求中我需要在sql上运行这个select语句并将这些值作为db结果获取..





需要帮助.. !!

Hi All,

I have a table A which has a column with some codes sperated by comma in the column data like below

Centercodenumbers
1,2,3,4


Another Table B with columns and values

CenterCode CenterName
1 --------------- India
2 ---------------- US
3 ---------------- Canada
4 ---------------- ..
5 ----------------- ..
.. ---------------- ..
50 ---------------- IreLand

Now I wanted to join the two tables and get another new column with the data like below

NewCenterValues
India, US, Canada

This had the code values above
Centercodenumbers
1,2,3,4



Can anyone please help..


I have done it using C# to display values like above in the front end using SPLIT(',')

But in this requirement i need to run this select statement on sql and get these values as a db result..


Help needed..!!

推荐答案

select stuff ((select ',' +cast( CenterName as nvarchar(max)) From tableb where centerCode in(1,2,3) for xml path ('')),1,1,'')







并学习 xml path


这篇关于SQL查询在1 db列中使用逗号分隔多个值以连接另一个表,并在以逗号分隔的新列中获取值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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