怎么用SQL语句将一张表中ID相同的行的内容合并在一起
tableIDTEXT1wearechinese1welikeourcountry1thankyou2you2are2ok要把这张表合并成IDTEXT1wearechin...
table
ID TEXT
1 we are chinese
1 we like our country
1 thank you
2 you
2 are
2 ok
要把这张表合并成
ID TEXT
1 we are chinese we like our country thank you
2 you are ok
用SQL语句怎么写 展开
ID TEXT
1 we are chinese
1 we like our country
1 thank you
2 you
2 are
2 ok
要把这张表合并成
ID TEXT
1 we are chinese we like our country thank you
2 you are ok
用SQL语句怎么写 展开
展开全部
for xml path 可以
代码如下
CREATE table TT(
id int,
Text1 varchar(120)
)
INSERT INTO TT SELECT 1,'we are chinese' union SELECT 1,' we like our country' union SELECT 1,' thank you' union
SELECT 1,'we are chinese' union select 2,' you ' union SELECT 2,'are' union SELECT 2,'ok'
select T.id,(SELECT Text1+' ' from TT where TT
.id=T.id FOR XML PATH('') )as Text1 from TT as T group by id
不懂可以私信我
代码如下
CREATE table TT(
id int,
Text1 varchar(120)
)
INSERT INTO TT SELECT 1,'we are chinese' union SELECT 1,' we like our country' union SELECT 1,' thank you' union
SELECT 1,'we are chinese' union select 2,' you ' union SELECT 2,'are' union SELECT 2,'ok'
select T.id,(SELECT Text1+' ' from TT where TT
.id=T.id FOR XML PATH('') )as Text1 from TT as T group by id
不懂可以私信我
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
要达到你说的结果, 单靠sql语句难度很大(你的这个相同id的数据行数不确定吧?). 给你个思路供参考: 先循环读出并合并相同id的数据, 然后再写入一新表中.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
看你什么数据库了额,oracle如下,其他数据库不好弄
select id,wm_concat(text) as Text
from table_name
group by id
select id,wm_concat(text) as Text
from table_name
group by id
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询