Mysql sql查询以获取参与的不同类的计数
我有一张游泳考勤表Mysql sql查询以获取参与的不同类的计数,mysql,sql,Mysql,Sql,我有一张游泳考勤表 student_name date_attended cnt Bob 21-Aug-2013 1 Bob 10-Sep-2013 1 student_name date_attended cnt Bob 20-Aug-2013 1 Bob 21-Aug-2013 1 Bob 21-Aug-2013 1 我有一张空手道考勤表
student_name date_attended cnt
Bob 21-Aug-2013 1
Bob 10-Sep-2013 1
student_name date_attended cnt
Bob 20-Aug-2013 1
Bob 21-Aug-2013 1
Bob 21-Aug-2013 1
我有一张空手道考勤表
student_name date_attended cnt
Bob 21-Aug-2013 1
Bob 10-Sep-2013 1
student_name date_attended cnt
Bob 20-Aug-2013 1
Bob 21-Aug-2013 1
Bob 21-Aug-2013 1
我正在统计每个学生的游泳和空手道出勤人数
student_name swim_attendance karate_attendance
Bob 2 3
这里有一个方法
select
student_name,
coalesce(sum(swim_attendance)) as swim_attendance,
coalesce(sum(karate_attendance)) as karate_attendance
from
(
(
select student_name,sum(cnt) as swim_attendance, 0 as karate_attendance
from swim_attendance group by student_name
)
union all
(
select student_name,0 as swim_attendance, sum(cnt) as karate_attendance
from karate_attendance group by student_name
)
)x
group by student_name
;
更新:从Straberry提出的点到sum()仅一次
select
student_name,
coalesce(sum(swim_attendance)) as swim_attendance,
coalesce(sum(karate_attendance)) as karate_attendance
from
(
(
select student_name,cnt as swim_attendance, 0 as karate_attendance
from swim_attendance
)
union all
(
select student_name,0 as swim_attendance, cnt as karate_attendance
from karate_attendance
)
)x
group by student_name
;
这里有一个方法
select
student_name,
coalesce(sum(swim_attendance)) as swim_attendance,
coalesce(sum(karate_attendance)) as karate_attendance
from
(
(
select student_name,sum(cnt) as swim_attendance, 0 as karate_attendance
from swim_attendance group by student_name
)
union all
(
select student_name,0 as swim_attendance, sum(cnt) as karate_attendance
from karate_attendance group by student_name
)
)x
group by student_name
;
更新:从Straberry提出的点到sum()仅一次
select
student_name,
coalesce(sum(swim_attendance)) as swim_attendance,
coalesce(sum(karate_attendance)) as karate_attendance
from
(
(
select student_name,cnt as swim_attendance, 0 as karate_attendance
from swim_attendance
)
union all
(
select student_name,0 as swim_attendance, cnt as karate_attendance
from karate_attendance
)
)x
group by student_name
;
这里有一个方法
select
student_name,
coalesce(sum(swim_attendance)) as swim_attendance,
coalesce(sum(karate_attendance)) as karate_attendance
from
(
(
select student_name,sum(cnt) as swim_attendance, 0 as karate_attendance
from swim_attendance group by student_name
)
union all
(
select student_name,0 as swim_attendance, sum(cnt) as karate_attendance
from karate_attendance group by student_name
)
)x
group by student_name
;
更新:从Straberry提出的点到sum()仅一次
select
student_name,
coalesce(sum(swim_attendance)) as swim_attendance,
coalesce(sum(karate_attendance)) as karate_attendance
from
(
(
select student_name,cnt as swim_attendance, 0 as karate_attendance
from swim_attendance
)
union all
(
select student_name,0 as swim_attendance, cnt as karate_attendance
from karate_attendance
)
)x
group by student_name
;
这里有一个方法
select
student_name,
coalesce(sum(swim_attendance)) as swim_attendance,
coalesce(sum(karate_attendance)) as karate_attendance
from
(
(
select student_name,sum(cnt) as swim_attendance, 0 as karate_attendance
from swim_attendance group by student_name
)
union all
(
select student_name,0 as swim_attendance, sum(cnt) as karate_attendance
from karate_attendance group by student_name
)
)x
group by student_name
;
更新:从Straberry提出的点到sum()仅一次
select
student_name,
coalesce(sum(swim_attendance)) as swim_attendance,
coalesce(sum(karate_attendance)) as karate_attendance
from
(
(
select student_name,cnt as swim_attendance, 0 as karate_attendance
from swim_attendance
)
union all
(
select student_name,0 as swim_attendance, cnt as karate_attendance
from karate_attendance
)
)x
group by student_name
;
你可以这样做
select s.student_name ,
sum(cnt * coalesce(s.table_type = 'swim') ) swim_attendance,
sum(cnt * coalesce(s.table_type = 'karate') ) karate_attendance
from (
select student_name , 'swim' as table_type,cnt from swim_attendance
union all
select student_name ,'karate' as table_type,cnt from karate_attendance
) s
group by s.student_name
你可以这样做
select s.student_name ,
sum(cnt * coalesce(s.table_type = 'swim') ) swim_attendance,
sum(cnt * coalesce(s.table_type = 'karate') ) karate_attendance
from (
select student_name , 'swim' as table_type,cnt from swim_attendance
union all
select student_name ,'karate' as table_type,cnt from karate_attendance
) s
group by s.student_name
你可以这样做
select s.student_name ,
sum(cnt * coalesce(s.table_type = 'swim') ) swim_attendance,
sum(cnt * coalesce(s.table_type = 'karate') ) karate_attendance
from (
select student_name , 'swim' as table_type,cnt from swim_attendance
union all
select student_name ,'karate' as table_type,cnt from karate_attendance
) s
group by s.student_name
你可以这样做
select s.student_name ,
sum(cnt * coalesce(s.table_type = 'swim') ) swim_attendance,
sum(cnt * coalesce(s.table_type = 'karate') ) karate_attendance
from (
select student_name , 'swim' as table_type,cnt from swim_attendance
union all
select student_name ,'karate' as table_type,cnt from karate_attendance
) s
group by s.student_name
没有两张桌子。你试过什么吗?我可以马上发布一个查询,但我想知道你的努力。为什么有一个
cnt
列?…另外,你没有主键。这是件坏事。没有两张桌子。你试过什么吗?我可以马上发布一个查询,但我想知道你的努力。为什么有一个cnt
列?…另外,你没有主键。这是件坏事。没有两张桌子。你试过什么吗?我可以马上发布一个查询,但我想知道你的努力。为什么有一个cnt
列?…另外,你没有主键。这是件坏事。没有两张桌子。你试过什么吗?我可以马上发布一个查询,但我想知道你的努力。为什么有一个cnt
列?…另外,你没有主键。这是一件坏事。我想只有在我明白你的意思后,求和才会更有效率!!我想只有当我明白你的意思时,求和才会更有效率!!我想只有当我明白你的意思时,求和才会更有效率!!我想只有当我明白你的意思时,求和才会更有效率!!