Voici une manière de combiner tes 2 requêtes grâce aux CTE (MySQL 8)
(ta syntaxe est à harmoniser)
WITH
match_count AS (
SELECT
count(compos.compo_num) AS TotalMatchSaison,
saison.sai_nom AS NomSaison,
c.comp_nom AS NomCompet
FROM compos
inner join mtchs on compos.mtch_id=mtchs.mtch_id
inner join saison on mtchs.id_saison=saison.sai_id
inner join joueurs on compos.joueur_id=joueurs.joueur_id
INNER JOIN compet AS c ON mtchs.id_compet=c.id_comp
WHERE joueurs.joueur_id = 167 and compos.compo_num<24
GROUP BY NomSaison, NomCompet
),
action_count AS (
select
saison.sai_nom AS NomSaison,
c.comp_nom AS NomCompet,
sum(case WHEN marqueurs.action_id=1 then 1 else 0 end) AS TotalEssais,
sum(case WHEN marqueurs.action_id=2 then 1 else 0 end) AS TotalTransf,
sum(case WHEN marqueurs.action_id=3 then 1 else 0 end) AS TotalPen,
sum(case WHEN marqueurs.action_id=4 then 1 else 0 end) AS TotalDrop,
sum(case WHEN marqueurs.action_id=5 then 1 else 0 end) AS TotalCJ,
sum(case WHEN marqueurs.action_id=6 then 1 else 0 end) AS TotalCR
from marqueurs
inner join mtchs on mtchs.mtch_id=marqueurs.mtch_id
inner join saison on saison.sai_id=mtchs.id_saison
INNER JOIN compet AS c ON mtchs.id_compet=c.id_comp
where marqueurs.joueur_id = 167
GROUP BY NomSaison, NomCompet
),
axe AS (
SELECT ALL NomCompet, NomSaison FROM match_count
UNION DISTINCT
SELECT ALL NomCompet, NomSaison FROM action_count
)
SELECT ALL a.NomCompet, a.NomSaison, mc.TotalMatchSaison, ac.TotalEssais, ac.TotalTransf, ...
FROM axe AS a
LEFT OUTER JOIN match_count AS mc ON (a.NomCompet, a.NomSaison) = (mc.NomCompet, mc.NomSaison)
LEFT OUTER JOIN action_count AS ac ON (a.NomCompet, a.NomSaison) = (ac.NomCompet, ac.NomSaison)
On peut probablement procéder sans CTE, mais sans schéma et jeu de données de test je ne m'y risquerai pas.
Partager