select t3.companyId,t3.companyName as companyName , t1.coachNum, t2.guideNum, t3.playerNum , IFNULL(t1.coachNum,0) + IFNULL(t2.guideNum,0) + IFNULL(t3.playerNum,0) as sum from
(coachNum+guideNum+playerNum) as sum
(select t.companyId, t.companyName,count(t.id) as playerNum from
FROM
(SELECT
(SELECT
tcc.`name` as companyName,
DISTINCT(tcp.id) as id, tcc.id as companyId,tcc.`name` as companyName
tcc.id,
FROM
(SELECT count(tcca.id) FROM t_compete_coach tcca WHERE tcc.id = tcca.company_id and identity = 1 and rec_status = 0) as coachNum,
t_compete_company tcc
(SELECT count(tcca.id) FROM t_compete_coach tcca WHERE tcc.id = tcca.company_id and identity = 0 and rec_status = 0) as guideNum,
left join
((SELECT COUNT(tcpp.id) FROM t_compete_project_player tcpp LEFT JOIN t_compete_player tcp on tcp.id = tcpp.player_id WHERE tcp.company_id = tcc.id and tcp.rec_status = 0 and tcpp.rec_status = 0 and tcp.rec_status = 0) +
t_compete_player tcp on tcc.id = tcp.company_id
(SELECT COUNT(tctm.id) FROM t_compete_team_member tctm LEFT JOIN t_compete_team tct on tctm.compete_team_id = tct.id WHERE tct.company_id = tcc.id and tct.rec_status = 0 and tctm.rec_status = 0 ))as playerNum
LEFT JOIN t_compete_project_player tcpp ON tcp.id = tcpp.player_id
WHERE
tcc.type = 0
AND tcp.company_id = tcc.id
AND tcc.rec_status = 0
AND tcp.rec_status = 0
AND tcpp.rec_status = 0
UNION
SELECT
DISTINCT(tcp.id) as id,tcc.id as companyId,tcc.`name` as companyName
FROM
FROM
t_compete_company tcc
t_compete_company tcc
LEFT JOIN t_compete_team tct ON tcc.id = tct.company_id
LEFT JOIN t_compete_team_member tctm on tct.id = tctm.compete_team_id
LEFT JOIN t_compete_player tcp ON tctm.player_id = tcp.id
WHERE
WHERE
tcc.rec_status =0
tcc.type = 0
and tcc.compete_time_id = #{competeTimeId}
AND tct.rec_status = 0
) c
AND tctm.rec_status = 0
WHERE c.playerNum <> 0
And tcc.rec_status = 0
AND tcp.rec_status = 0) t
group by t.companyId
having count(t.id) > 0
) t3
left join
(SELECT tcc.id as companyId,tcc.`name` as companyName ,count(tcca.id) as coachNum FROM t_compete_company tcc left join (select * from t_compete_coach where rec_status = 0) tcca on tcc.id = tcca.company_id where tcc.type = 0 AND identity = 1 and tcc.rec_status = 0 GROUP BY tcc.id) t1 on t1.companyId = t3.companyId
left join
(SELECT tcc.id as companyId,tcc.`name` as companyName , count(tcca.id) as guideNum FROM t_compete_company tcc left join (select * from t_compete_coach where rec_status = 0) tcca on tcc.id = tcca.company_id where tcc.type = 0 AND identity = 0 and tcc.rec_status = 0 GROUP BY tcc.id) t2 on t2.companyId = t3.companyId