首页 > 数据库技术 > 详细

SQL 查询学习

时间:2014-10-16 00:02:42      阅读:355      评论:0      收藏:0      [点我收藏+]

1、表数据如下
ID     stuid   status
1 100      1
3 200      1
4 2343    1
5 52      3
6 42      5
7 333      1

想得到下面结果
stuid                                总数
100,200,2343,333         4

首先创建试验表

1 with tablea as 
2 (
3 select 1 as id,100 as stuid,1 as status union all 
4 select 3 ,200,1 union all 
5 select 4 ,2343,1 union all 
6 select 5 ,52,3 union all 
7 select 6 ,42,5 union all
8  select 7 ,333,1 ) 

SQL 语句实现

SELECT stuff(Select ‘,‘ +convert(nvarchar(100),stuid) from tablea where status=1 order by stuid FOR XML PATH(‘‘)),1,1,‘‘)

AS stuid,count(*)  as 总数 from tablea as a where status=1

SQL 查询学习

原文:http://www.cnblogs.com/brave-cz/p/4027727.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!