PHP ver gevorderde |
|
Die query staat ook in mijn startpost.
Een subquery mag (op die manier) geen meerdere resultaten weergeven.
Voor al de geïnteresseerden, mijn query ziet er zo uit momenteel:
SELECT games_game.titel AS titel, games_game.id AS id, games_cat.titel AS cat, games_game.zichtbaar AS zichtbaar,
SUBSTRING_INDEX(GROUP_CONCAT(DISTINCT CONCAT(games_scores.score,CONCAT(';',games_scores.user)) ORDER BY games_scores.score DESC, games_scores.id ASC),',',10) AS scores,
SUBSTRING_INDEX(GROUP_CONCAT(DISTINCT CONCAT(leden.id,CONCAT(';',CONCAT(leden.naam,CONCAT(';',leden.groep)))) ORDER BY games_scores.score DESC, games_scores.id ASC),',',10) AS users,
SUBSTRING_INDEX(GROUP_CONCAT(DISTINCT CONCAT(groepen.id, CONCAT(';',groepen.class)) ORDER BY groepen.id ASC),',',10) AS classes,
(SELECT SUM(games_scores.aantal) AS gespeeld FROM games_scores WHERE games_scores.game = games_game.id) AS gespeeld,
(SELECT SUM(games_scores.aantal) AS gespeeld_maand FROM games_scores
WHERE games_scores.game = games_game.id AND MONTH(datum) = MONTH(NOW()) AND YEAR(datum) = YEAR(NOW())) AS gespeeld_maand
FROM games_game
LEFT JOIN games_cat ON (games_game.cat = games_cat.id)
LEFT JOIN games_scores ON (games_game.id = games_scores.game)
LEFT JOIN leden ON (games_scores.user = leden.id)
LEFT JOIN groepen ON (leden.groep = groepen.class)
WHERE games_cat.titel = 'Actie' AND MONTH(games_scores.datum) = MONTH(NOW()) AND YEAR(games_scores.datum) = YEAR(NOW())
GROUP BY games_game.id
ORDER BY gespeeld_maand DESC
LIMIT 10;
SELECT games_game.titel AS titel, games_game.id AS id, games_cat.titel AS cat, games_game.zichtbaar AS zichtbaar, SUBSTRING_INDEX(GROUP_CONCAT(DISTINCT CONCAT(games_scores.score,CONCAT(';',games_scores.user)) ORDER BY games_scores.score DESC, games_scores.id ASC),',',10) AS scores, SUBSTRING_INDEX(GROUP_CONCAT(DISTINCT CONCAT(leden.id,CONCAT(';',CONCAT(leden.naam,CONCAT(';',leden.groep)))) ORDER BY games_scores.score DESC, games_scores.id ASC),',',10) AS users, SUBSTRING_INDEX(GROUP_CONCAT(DISTINCT CONCAT(groepen.id, CONCAT(';',groepen.class)) ORDER BY groepen.id ASC),',',10) AS classes, (SELECT SUM(games_scores.aantal) AS gespeeld FROM games_scores WHERE games_scores.game = games_game.id) AS gespeeld, (SELECT SUM(games_scores.aantal) AS gespeeld_maand FROM games_scores WHERE games_scores.game = games_game.id AND MONTH(datum) = MONTH(NOW()) AND YEAR(datum) = YEAR(NOW())) AS gespeeld_maand FROM games_game LEFT JOIN games_cat ON (games_game.cat = games_cat.id) LEFT JOIN games_scores ON (games_game.id = games_scores.game) LEFT JOIN leden ON (games_scores.user = leden.id) LEFT JOIN groepen ON (leden.groep = groepen.class) WHERE games_cat.titel = 'Actie' AND MONTH(games_scores.datum) = MONTH(NOW()) AND YEAR(games_scores.datum) = YEAR(NOW()) GROUP BY games_game.id ORDER BY gespeeld_maand DESC LIMIT 10;
Deze query geeft het gewenste resultaat, maar is alles behalve ideaal geschreven.
(Een nadeel is dat GROUP_CONCAT geen LIMIT ondersteunt)
Kan er iemand deze nog verbeteren/optimaliseren? Of zal het zo blijven? |