sql查询在一行中获取多个记录 [英] sql query to get multiple records in one row

查看:93
本文介绍了sql查询在一行中获取多个记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我要选择记录





从tbl_records中选择id


这样的结果是




1

2

3

4

5

6

7



我怎么样这个



排成一列





1 2 3 4 5 6 7

if i am going to select Records


select id from tbl_records

the result like this

1
2
3
4
5
6
7

how do i get like this

row into column


1 2 3 4 5 6 7

推荐答案

试试这个
SELECT STUFF((SELECT ',' + cast(ID as varchar) AS [text()]
  FROM tbl_records  FOR XML PATH('') ), 1, 1, '' )



逗号分隔,如果不想要你可以删除它;)


its a comma separated if dont want you can remove it ;)


PIVOT 将帮助你。!

什么是PIVOT [ ^ ]它是示例 [ ^ ]。
PIVOT will help you.!
See what is PIVOT[^] and it's examples[^].


只需添加,这里 [ ^ ]是关于pivot,for xml的文章,以及你需要做的其他选项。



特别重要的是要注意,'FOR XML'选项是唯一有效的选项,如果您在开始之前不知道,您需要提取的值列表是什么。
Just to add, here[^] is my article on pivot, for xml, and the other options you have to do what you want.

It's especially important to note that the 'FOR XML' option is the only one that works if you don't know before you start, what your list of values you want pulled out is.


这篇关于sql查询在一行中获取多个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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