I want take the result from scheda_atleta table for every player and make an HTML table with the best result. I've two tables:
first named: at
+----+----------------+ | id | nome_e_cognome | +----+----------------+ | 1 | Tom Tom | | 2 | Jerry | +----+----------------+ and second: scheda_atleta
+----+--------+----------+----------------+-----------+ | id | atleta | fs_score | qualificazione | categoria | +----+--------+----------+----------------+-----------+ | 1 | 1 | 11,25 | 1 | 1 | | 2 | 1 | 10,55 | 2 | 1 | | 3 | 2 | 14,32 | 1 | 1 | | 4 | 2 | 12,33 | 3 | 1 | +----+--------+----------+----------------+-----------+ I try this PHP code below, but is not work:
$db = JFactory::getDBO(); $bestAthleteScores = $db->getQuery(true) ->select($db->qn(["scores1.atleta", "scores1.fs_score","scores1.qualificazione","scores1.categoria"])) ->from($db->qn("scheda_atleta", "scores1")) ->leftJoin($db->qn("scheda_atleta", "scores2") . " ON " . $db->qn("scores1.atleta") . " = " . $db->qn("scores2.atleta") . " AND " . $db->qn("scores1.fs_score") . " < " . $db->qn("scores2.fs_score") ) ->where($db->qn("scores2.fs_score") . " IS NULL") ->where($db->qn("scores1.qualificazione") ."=". 1) ->where($db->qn("scores1.categoria"). ' IN (' . implode(',', $db->quote(array(1,99,100))) . ')' ) ->order($db->qn("scores1.fs_score") . " DESC"); $rankVariable = $db->getQuery(true) ->select("@i := 0"); $rankedBestScores = $db->getQuery(true) ->select($db->qn("fs_score") . ",@i := @i + 1 AS " . $db->qn("rank")) ->from("(" . $bestAthleteScores . ") AS " . $db->qn("best_athlete_scores")) ->join("CROSS", "(" . $rankVariable . ") AS " . $db->qn("rank_variable")) ->order($db->qn("best_athlete_scores.fs_score") . " DESC"); $bestScoreRanks = $db->getQuery(true) ->select($db->qn("fs_score") . ", MIN(" . $db->qn("rank") . ") AS " . $db->qn("rank")) ->from("(" . $rankedBestScores . ") AS " . $db->qn("ranked_best_scores")) ->group($db->qn("fs_score")); $query = $db->getQuery(true) ->select( $db->qn( ["best_score_ranks.rank", "athlete_profile.nome_e_cognome", "athlete_profile.id", "athlete_scores.fs_score"], [null, "athlete_name", "athlete_id", "best_score"] ) ) ->from($db->qn("at", "athlete_profile")) ->innerJoin($db->qn("scheda_atleta", "athlete_scores") . " ON " . $db->qn("athlete_profile.id") . " = " . $db->qn("athlete_scores.atleta")) ->innerJoin("(" . $bestScoreRanks . ") AS " . $db->qn("best_score_ranks") . " ON " . $db->qn("athlete_scores.fs_score") . " = " . $db->qn("best_score_ranks.fs_score")) ->where($db->qn("qualificazione")." =". 1) ->where($db->qn("categoria") . ' IN (' . implode(',', $db->quote(array(1,99,100))) . ')' ) ->group($db->qn("athlete_profile.id")) ->order($db->qn("rank")); $query->setLimit(24); $db->setQuery($query); $db->execute(); $query = $db->loadAssocList(); //echo "<pre>" , var_export($db->loadAssocList(), true) , "</pre>"; ?> <table class="category table table-striped table-bordered table-hover table-noheader"> <tr> <th>nr</th> <th>Atleta</th> <th>RANK</th> </tr> <tbody> <tr> <?php foreach ($query as $result){ ?> <td><?php echo($result['rank']); ?></td> <td><?php echo($result['athlete_name']); ?></td> <td><?php echo($result['best_score']); ?> </td> </tr><?php } ?> </tbody></table> I would like to get this HTML table result (best result of 4 races): +-----+-------------+------------+ | nr. | Player Name | Best score | +-----+-------------+------------+ | 1 | Jerry | 14,32 | | 2 | Tom Tom | 11,25 | +-----+-------------+------------+ etc. But my code doesn't work, skip some data . Where am I wrong?
Joomla! 3.9.21 Stable and MySql 5.7
This raw sql would work if my db version supported RANK() OVER():
SELECT RANK() OVER (ORDER BY MAX(fs_score) desc) AS nr, nome_e_cognome AS "Player Name", MAX(fs_score) AS "Best score" FROM scheda_atleta INNER JOIN at on at.id=scheda_atleta.atleta GROUP BY at.id, nome_e_cognome but I want to generate the same result set using Joomla's query building helper methods.