SELECT AVG(rating)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi - I have a MySQL table full of product reviews and I'm trying to select
info for only the top 5 rated products.  The only way I can figure out how
to do it so far is something like:

$query1 = mysql_query("SELECT * FROM products");
for($i=1;$i<=mysql_num_rows($query1);$i++) {
	$row1 = mysql_fetch_array($query1,MYSQL_ASSOC);
	$query2 = mysql_query("SELECT AVG(rating) as rating FROM reviews
WHERE product_id='" . $row1['product_id'] . "'");
	...
	$product[$i]['name'] = $row1['product_name'];
	$product[$i]['rating'] = $row2['rating'];
}

And then use array functions to sort and display only the first 5.

Is there any easier way to get this done with a single query - something
like "SELECT AVG(rating) WHERE product_id=DISTINCT(product_id)"? <<= I tried
that - it didn't work.  Would greatly appreciate any advice.  Thanks,

Ben 



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux