Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Select Multiple Columns based on Max Value



8:39 am on Mar 8, 2012 (gmt 0)

5+ Year Member


I have a database with around 50 columns, 1 column is "name" and the other columns are "item1, item2, item3" and so on.

I've got the following script to select the highest value from the item columns:

$sql1 = "SELECT max(item1), name FROM pricelist";
$result1 = mysql_query($sql1) or die(mysql_error());
$row1 = mysql_fetch_array($result1);
$item1_1 = $row1['max(item1)'];
$item1_2 = $row1['name'];

It works fine but it isn't selecting the correct name.. I think it's because I'm not telling it which name to actually select.

Hopefully someone can help..


7:15 am on Mar 9, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

Try this instead (untested):
$max = mysql_fetch_row(mysql_query("SELECT max(item1) FROM pricelist"));
$sql1 = "SELECT name FROM pricelist WHERE item1 = '{$max[0]}'";
$result1 = mysql_query($sql1) or die(mysql_error());
$row1 = mysql_fetch_row($result1);
$item1_1 = $max[0];
$item1_2 = $row1[0];


1:48 pm on Mar 9, 2012 (gmt 0)

5+ Year Member

Works perfect, thanks very much.

Featured Threads

Hot Threads This Week

Hot Threads This Month