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

统计ID出现的次数,求SQL
有一个表的结构是这样的:
fOne   int
fTwo   int
内容是这样的:
fOne   fTwo
1             2
1             3
2             4
4             2
我想统计的结果是各元素出现在fOne或fTwo的次数:
结果应是:
FID       TOTAL
2             3
1             2
3             1
4             2
请问各位怎么写SQL?

------解决方案--------------------
select fid,count(*) total from
(
select fOne fid from 表
union
select fTwo fid from 表
) a
group by fid
------解决方案--------------------
select a as FID,count(1) as TOTAL from (select fOne as a from tablename union all select fTwo as a from tablename) t group by a