Forum Moderators: coopster

Message Too Old, No Replies

MAX and GROUP BY

Having problem shoving the right value

         

sengir

12:09 am on Oct 9, 2006 (gmt 0)

10+ Year Member



Hi. i want to show whats in the navn column of the row with the highest id of each group. here is my code:

$query = "SELECT MAX(id), postid, navn FROM gblondon065post WHERE kategori='info' GROUP BY postid";

$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result)){
echo $row['navn'];

But it wont work. When i use echo $row['MAX(id)']; the highest id shows, but i want to echo the 'navn' and not the 'id'

Hope someone understands, im not that good in english :P

MattAU

12:38 am on Oct 9, 2006 (gmt 0)

10+ Year Member



You can use:

echo $row[0];

or you can give the MAX(id) an alias in the sql statement:

$query = "SELECT MAX(id) as 'temp_max', postid, navn FROM gblondon065post WHERE kategori='info' GROUP BY postid";

echo $row['temp_max'];

coopster

8:17 pm on Oct 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, sengir.

You should also add columns from your list into your GROUP BY clause. MySQL is one database that allows this as an extension to the standard but it is not good practice.

[dev.mysql.com...]