如何从Sql Server-2005中的两个表中获取逗号分隔的值 [英] how to get comma separated values from two tables in Sql Server-2005
问题描述
我有两个表,它们是
1)学生(student_no(主键),student_name)
2)phoneDetails(student_no(外键,电话号码)
学生表具有这样的记录
1 raj
2拉尼
3巴布
phonedetails表具有这样的记录
1 3434556
2 4555667
1 4567566
3 5678908
2 5623468
2 1234565
一个学生可以有多个电话号码,我想用逗号分隔的值来获取这些电话号码,
我想得到的结果是
1 raj 3434556,4567566
2 rani 4555667,5623468,1234565
3 babu 5678908
请通过SQL Server查询帮助我.
I have two tables they are
1) student(student_no (primary Key) ,student_name)
2) phoneDetails(student_no (Foreign Key,phone no)
student table has the records like this
1 raj
2 rani
3 babu
phonedetails table has the records like this
1 3434556
2 4555667
1 4567566
3 5678908
2 5623468
2 1234565
A student can have multiple no of phone numbers,I would like to get these numbers in comma separated values,
I would like to get the result as
1 raj 3434556,4567566
2 rani 4555667,5623468,1234565
3 babu 5678908
Please help me through SQL Server queries.
推荐答案
您的表"student"和"phonedetails"与列名相关
''student_no''.因此,您可以通过联接2个表来查询所需的信息.一旦你
得到结果,执行"string.format"以按照需要的方式得到结果.
要联接表,请使用sql语句内部联接"或联接".
Your tables ''student'' and ''phonedetails'' are related with column name
''student_no''. So you can query required information by joining 2 tables. Once you
get the result, do ''string.format'' to get the result the way you need.
To join the tables use sql statements ''inner join'' or ''join''.
这篇关于如何从Sql Server-2005中的两个表中获取逗号分隔的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!