日期:2014-05-18  浏览次数:20551 次

高手请进,记录个数的查询问题
是这样的,我的数据表中有字段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