在单列中串联记录而不循环? [英] Concatenating records in a single column without looping?

查看:44
本文介绍了在单列中串联记录而不循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张带有1列varchar值的表.我正在寻找一种方法,将这些值连接成一个没有循环的单个值(如果可能).如果循环是解决此问题的最有效方法,那么我将采用这种方式,但认为在默认使用该方法之前,我会要求其他选项.我也想将其保留在SQL查询中.

I have a table with 1 column of varchar values. I am looking for a way to concatenate those values into a single value without a loop, if possible. If a loop is the most efficient way of going about this, then I'll go that way but figured I'd ask for other options before defaulting to that method. I'd also like to keep this inside of a SQL query.

最终,我想做一个拆分函数的相反操作.

Ultimately, I want to do the opposite of a split function.

是否可以不使用循环(或游标),还是应该仅使用循环来实现这一目标?

Is it possible to do without a loop (or cursor) or should I just use a loop to make this happen?

由于在MySql中有一个很好的答案(与我最初打算的MS Sql相反),我决定重新标记以便其他人也可以找到答案.

Since there was a very good answer associated with how to do it in MySql (as opposed to MS Sql like I initially intended), I decided to retag so others may be able to find the answer as well.

推荐答案

声明@concat varchar(max) 设置@concat =''

declare @concat varchar(max) set @concat = ''

选择@concat = @concat + col1 +',' 来自tablename1

select @concat = @concat + col1 + ',' from tablename1

这篇关于在单列中串联记录而不循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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