7,714
社区成员
发帖
与我相关
我的任务
分享
select 数字,count(*) as 次数
from
(
select n1 as 数字 from db
union all
select n2 as 数字 from db
union all
select n3 as 数字 from db
union all
select n4 as 数字 from db
union all
select n5 as 数字 from db
union all
select n6 as 数字 from db
union all
select n7 as 数字 from db
) as t
group by 数字
--或者用楼上说的left join + right join 方式
这两种方式都可以
select 数字,count(*) as 次数
from
(
select n1 as 数字 from db
union all
select n2 as 数字 from db
union all
select n3 as 数字 from db
union all
select n4 as 数字 from db
union all
select n5 as 数字 from db
union all
select n6 as 数字 from db
union all
select n7 as 数字 from db
) as t
group by 数字
select n1 ,count(id) as cnt group by n1
union all select n2 ,count(id) as cnt group by n2
union all select n3 ,count(id) as cnt group by n3
union all select n4 ,count(id) as cnt group by n4
union all select n5 ,count(id) as cnt group by n5
union all select n6 ,count(id) as cnt group by n6
union all select n7 ,count(id) as cnt group by n7