首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

,记录个数的查询有关问题

2012-01-18 
高手请进,记录个数的查询问题是这样的,我的数据表中有字段1,2,3,4,5,6,7,它们的值分别都是1~35的数字,我想

高手请进,记录个数的查询问题
是这样的,我的数据表中有字段1,2,3,4,5,6,7 , 它们的值分别都是1~35的数字,我想知道1~35每个数字在字段1~5中总共出现几次,这样的SQL语句应该怎么写? 
比如说 

字段1 字段2 字段3 字段4 字段5 字段6 字段7  
1 3 5 6 16 30 33 
4 5 8 9 12 34 35 

如果是这样的话,我要得出的是1~35 每个数字在字段1~5(总共有7个字段,但搜索范围是5个字段)中出现的次数,比如1:2次.... 5:2次 应该怎么写查询语句???在线等。

[解决办法]
少敲了个from 
select id,count(*) from
(select f1 from tb 
union all 
select f2 from tb 
union all 
select f3 from tb 
union all 
select f4 from tb 
union all 
select f5 from tb) d 
inner join
(select 1 id union all select 2 union all select .... union all select 35) x 
on f1=id 
group by id 


或者直接


select id,count(*) from
(select f1 from tb 
union all 
select f2 from tb 
union all 
select f3 from tb 
union all 
select f4 from tb 
union all 
select f5 from tb) d where f1 between 1 and 35

热点排行