如何将字符串行合并为一列

huangapple go评论64阅读模式
英文:

How to combined String rows to one column

问题

data A data B
apple apple, banana, cat
banana apple, banana, cat
cat apple, banana, cat

如果我有类似“data A”列的数据,并且我想将这些数据与另一列(如“data B”)合并,我可以如何查询并显示我想要的结果?

抱歉,我的SQL语句可能有点生疏。

英文:
data A data B
apple apple, banana, cat
banana apple, banana, cat
cat apple, banana, cat
SELECT 
  data A,
  '' as data B
From tbl_test;

If I have data like Column data A and I want to combined this data to another column like data b

How can I query this one and show like I want

Sorry for my news sql

答案1

得分: 0

通过使用STUFF,我们可以通过编写子查询将所有列数据合并为单个列,适用于SQL Server 2014。代码如下。

select dataA, STUFF((
    select ',' + r.dataA
    from TestTable as r
    for xml path('')), 1, 1, '') as dataB From TestTable
英文:

By using STUFF we can combine all column data into single column by writing Subquery it will give's Result to all columns suitable for SQL Server 2014. Code is as follows.

select dataA,STUFF((
select ','+r.dataA
from TestTable as r
for xml path('')),1,1,'') as dataB From TestTable

huangapple
  • 本文由 发表于 2023年7月11日 12:27:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658716.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定