求教一个SQL的写法

woshizhaoxuhui 2013-11-12 03:23:03
如下的一个表:
name age
jack 20
tom 30
nancy 20

现在我想把所有年龄为20的人的姓名选择到一条记录里,得到这样的结果
jack,nancy
也就是把符合条件的记录中的name字段的值拼接起来,
不知道能否办到?
...全文
136 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
人间太皮 2013-11-12
  • 打赏
  • 举报
回复
引用 6 楼 t101lian 的回复:
if object_id ('tempdb..temp','#aa') is not null
drop table #aa
go 
create table #aa
([name] varchar(5),
[age] int)
 
insert #aa select  'jack',20 union all
select 'tom',30 union all
select 'nancy',20
 
declare @name varchar(20)
select @name=isnull(@name+',','')+ name from #aa where age=20
print @name  
 

/*
jack,nancy
*/
@name长度有限制,不太好。。
t101lian 2013-11-12
  • 打赏
  • 举报
回复
if object_id ('tempdb..temp','#aa') is not null
drop table #aa
go 
create table #aa
([name] varchar(5),
[age] int)
 
insert #aa select  'jack',20 union all
select 'tom',30 union all
select 'nancy',20
 
declare @name varchar(20)
select @name=isnull(@name+',','')+ name from #aa where age=20
print @name  
 

/*
jack,nancy
*/
woshizhaoxuhui 2013-11-12
  • 打赏
  • 举报
回复
学习了,谢谢各位的指点!
  • 打赏
  • 举报
回复
是这样不:
--drop table tb

create table tb([name] varchar(5),[age] int)

insert tb select  'jack',20 union all
select 'tom',30 union all
select 'nancy',20


select  distinct
        --age,
        stuff(
               (
                 select ','+name
                 from tb t2
                 where t1.age = t2.age
                 for Xml path('')
               ),
               1,1,''
             ) as name
from tb t1  
where age =20
/*
name
jack,nancy
*/
小魚人 2013-11-12
  • 打赏
  • 举报
回复

create table #tb([name] varchar(5),[age] int)
insert #tb select  'jack',20 union all
select 'tom',30 union all
select 'nancy',20

select * from #tb 
declare @sql varchar(200)=''
select  @sql =@sql+','+name from #tb  where age ='20'
select stuff(@sql ,1,1,'')

drop table #tb
lzw_0736 2013-11-12
  • 打赏
  • 举报
回复

--創建數據
create table #temp([name] varchar(5),[age] int)
insert #temp
select 'jack',20 union all
select 'tom',30 union all
select 'nancy',20

--开始查询
SELECT stuff((select ','+[name] from #temp WHERE [age]=20 for xml path('')),1,1,'') [name]
發糞塗牆 2013-11-12
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author  :DBA_Huangzj(發糞塗牆)
-- Date    :2013-11-12 15:23:42
-- Version:
--      Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64) 
--	Dec 28 2012 20:23:12 
--	Copyright (c) Microsoft Corporation
--	Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go 
create table [huang]([name] varchar(5),[age] int)
insert [huang]
select 'jack',20 union all
select 'tom',30 union all
select 'nancy',20
--------------开始查询--------------------------

select a.[age],
stuff((select ','+[name] from [huang] b 
       where b.[age]=a.[age] 
       for xml path('')),1,1,'') 'name'
from [huang] a
WHERE [age]=20
group by  a.[age]
----------------结果----------------------------
/* 
age         name
----------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
20          jack,nancy
*/

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧