求一条查询话语,把某列的所有值,组成一个字符串返回
求一条查询语句,把某列的所有值,组成一个字符串返回
有一表,表中的数据如下
xh(nvarchar(6)) no(nvarchar(2))
110886 26
080940 26
050124 05
050125 05
050135 05
070278 05
050127 19
050128 19
050129 19
050130 19
050132 19
现在要返回xh 列所有的值,但是返回一个字符串,即希望得到的结果
'110886','080940','050124','050125','050135','070278','050127','050128','050129','050130','050132'
希望用一条 sql 语句实现
------解决方案--------------------
这样?你的结果和no是不是得有点关系啊?
------解决方案--------------------
有一表,表中的数据如下
xh(nvarchar(6)) no(nvarchar(2))
110886 26
080940 26
050124 05
050125 05
050135 05
070278 05
050127 19
050128 19
050129 19
050130 19
050132 19
现在要返回xh 列所有的值,但是返回一个字符串,即希望得到的结果
'110886','080940','050124','050125','050135','070278','050127','050128','050129','050130','050132'
希望用一条 sql 语句实现
sql 所有值 字符串 列
------解决方案--------------------
select distinct a=stuff((select ','''+convert(varchar,xh)+'''' from 表名 for xml path('')),1,1,'')
from 表名
这样?你的结果和no是不是得有点关系啊?
------解决方案--------------------
----------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-03-20 17:00:49
-- Version:
-- Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (X64)
-- Jun 17 2011 00:54:03
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1, v.721)
--
----------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([xh] varchar(6),[no] varchar(2))
insert [huang]
select '110886','26' union all
select '080940','26' union all
select '050124','05' union all
select '050125','05' union all
select '050135','05' union all
select '070278','05' union all
select '050127','19' union all
select '050128','19' union all
select '050129','19' union all
select '050130','19' union all