select count(b.name) as mary出现的次数
from
(
select a.name
from
(
select 姓名1 as name from 表
union all
select 姓名2 from 表
union all
select 姓名3 from 表
union all
select 姓名4 from 表
union all
select 姓名5 from 表
)a
where a.name = 'mary'
)b
select count(1) from table_name where 姓名1 = 'mary' +
select count(1) from table_name where 姓名2 = 'mary' +
select count(1) from table_name where 姓名3 = 'mary'+
select count(1) from table_name where 姓名4 = 'mary'+
select count(1) from table_name where 姓名5 = 'mary'
select count(*)
from (
select name1 from table_name
union all
select name2 from table_name
union all
select name3 from table_name
union all
select name4 from table_name
union all
select name5 from table_name
) t
where name1='mary'
笨而简单的方法,想一步完成,写个函数吧
; with aa as
(
Select a = count(姓名1='mary'),
b = count(姓名2='mary'),
c = count(姓名3='mary'),
d = count(姓名4='mary'),
e = count(姓名5='mary')
From 表
)
Select cnt = aa.a + aa.b + aa.c + aa.d + aa.e
From aa
--满意请采纳
select count(case when name1='mary' then 1 end)+...+count(case when name5='mary' then 1 end)
from table
select count(*)
from (
select name1 from table_name
union all
select name2 from table_name
union all
select name3 from table_name
union all
select name4 from table_name
union all
select name5 from table_name
) t
where name1='mary'