一道sql面试题解法

id  strvalue type
1    how      1
2    are      1
3    you      1
4    fine     2
5    thank    2
6    you      2

要求用sql把它们搜索出来成为这样的
#how are you#fine thank you#

 

解答:

select
(select '#'+replace(
  replace((SELECT strvalue FROM tb_test t where type = 1 FOR XML AUTO),'<t strvalue="',' ')
  ,'"/>', ' ')
   +'#'
)
+
(select replace(replace((SELECT strvalue FROM tb_test t where type = 2 FOR XML AUTO),'<t strvalue="',' '),'"/>', ' ')+'#')

 

posted @ 2009-10-30 11:18  superstar  阅读(229)  评论(0编辑  收藏  举报