Order by variables with another query?

I want to order by highest numbers in variables, like in this image: https://ibb.co/CVh4n5L , how i can do it? Or if you have better solutions… Please explain and show me examples. Here is my code:

$num_a1 = $conn->query(‘SELECT COUNT(),a_topics.forum_id,a_forums.id,a_forums.parent_id,a_forums.gameid FROM a_topics JOIN a_forums ON a_topics.forum_id = a_forums.id WHERE a_forums.gameid = 63’)->fetchColumn();
$num_a2 = $conn->query('SELECT COUNT(
),a_topics.forum_id,a_forums.id,a_forums.parent_id,a_forums.gameid FROM a_topics JOIN a_forums ON a_topics.forum_id = a_forums.id WHERE a_forums.gameid = 4’)->fetchColumn();

$stmt = $conn->prepare(‘SELECT id,name,parent_id,thisisgame,f_img FROM a_forums WHERE parent_id = 1 AND thisisgame = 1 GROUP BY id ORDER BY $num ? … ASC’);
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $row) {
echo “Name: $row[1] Topics:”;
switch (true) {
case $row[0] === ‘63’ :
echo “$num_a1”;
break;
case $row[0] === ‘4’ :
echo “$num_a2”;
break;
}

It doesn’t really make sense to send two queries when all they differ in is an ID. You could just use an IN clause with some GROUP BY

http://www.postgresqltutorial.com/postgresql-in/
And why would you order by $num? And what is $num? you could use additional JOINs or Subselects to get the counter within the third statement.

order by $num should all those variables, $num_a1,$num_a2,$num_a3, etc. i tried different solutions to order by nums but not work… so… maybe someone know how to do it…

Sponsor our Newsletter | Privacy Policy | Terms of Service