SQL 数据库中如何统计指定数据的个数 高分急寻

2024-12-16 17:48:08
推荐回答(5个)
回答1:

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

回答2:

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'
笨而简单的方法,想一步完成,写个函数吧

回答3:

; 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 
--满意请采纳

回答4:

select count(case when name1='mary' then 1 end)+...+count(case when name5='mary' then 1 end)
from table

回答5:

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'