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
(select t.companyId, t.companyName,count(t.id) as playerNum from
(SELECT
DISTINCT(tcp.id) as id, tcc.id as companyId,tcc.`name` as companyName
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
t_compete_company tcc
left join
t_compete_player tcp on tcc.id = tcp.company_id
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
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
tcc.type = 0
AND tct.rec_status = 0
AND tctm.rec_status = 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
(
SELECT
t.companyId,
t.companyName,
count( t.id ) AS playerNum
FROM
(
SELECT DISTINCT
( tcp.id ) AS id,
tcc.id AS companyId,
tcc.`name` AS companyName
FROM
t_compete_company tcc
LEFT JOIN t_compete_player tcp ON tcc.id = tcp.company_id AND tcp.rec_status = 0
LEFT JOIN t_compete_project_player tcpp ON tcp.id = tcpp.player_id AND tcpp.rec_status = 0
WHERE
tcc.type = #{competeTimeId}
AND tcc.rec_status = 0
UNION
SELECT DISTINCT
( tcp.id ) AS id,
tcc.id AS companyId,
tcc.`name` AS companyName
FROM
t_compete_company tcc
LEFT JOIN t_compete_team tct ON tcc.id = tct.company_id AND tct.rec_status = 0
LEFT JOIN t_compete_team_member tctm ON tct.id = tctm.compete_team_id AND tctm.rec_status = 0
LEFT JOIN t_compete_player tcp ON tctm.player_id = tcp.id AND tcp.rec_status = 0
WHERE
tcc.type = #{competeTimeId}
AND tct.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
AND tcca.rec_status = 0
WHERE
tcc.type = #{competeTimeId}
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