Jump to content

Recommended Posts

Is it possible order by an average from a field thats in a completely different table without an inner join?

 

Heres why im asking to see why the inner join is not a good choice if i can avoid it....

 

<?php
If($_GET['Sort'] == 1){
$Extra = 'ORDER BY GameName DESC';
}ElseIf($_GET['Sort'] == 2){
//some how .. order by AVG(Rating) FROM table "ratings" joined by GameID on ads table and ratings table...

}

$SELECT = mysql_querY("SELECT *
		FROM ads
		WHERE Authorised='1' AND Type='$Type' AND Status='$Status'
		$Extra")
Or die(mysql_error());
}
?>

 

Any ideas how i can do this ?

Link to comment
https://forums.phpfreaks.com/topic/162453-solved-order-by/
Share on other sites

Hi

 

Without doing a seperate piece of SQL with a JOIN (which would be my choice) the only thing I can think of would be to replace the select from ads with some kind of view which already joins ads and ratings, or to just always do the join but only sort by it when required.

 

Assuming GameId is a unique key on the ads table, something like this:-

 

<?php
If($_GET['Sort'] == 1)
{
$Extra = 'ORDER BY GameName DESC';
}
ElseIf($_GET['Sort'] == 2)
{
$Extra = 'ORDER BY GameAverageRating DESC';
}

$SELECT = mysql_querY("SELECT a.*, AVG(b.Rating) GameAverageRating
		FROM ads a
		LEFT OUTER JOIN ratings b
		ON a.GameId = b.GameId
		WHERE Authorised='1' AND Type='$Type' AND Status='$Status'
		GROUP BY a.GameId
		$Extra")
Or die(mysql_error());
}
?>

 

All the best

 

Keith

Link to comment
https://forums.phpfreaks.com/topic/162453-solved-order-by/#findComment-857513
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.