Log in

View Full Version : select from database where highest value



keyboard
01-20-2012, 08:53 AM
Hello everyone,

I've got a database named dictionary with four fields -
id
word
hash
times

The insert code looks like this



$time = time();
$sql = mysql_query ("INSERT INTO dictionary (id,word,hash,times) VALUES ('0','".$word."','".$hash."','".$time."')");

I'd like to select the entry that has the highest times field, and then echo the word and hash fields.

Any help?

traq
01-20-2012, 03:20 PM
SELECT * FROM dictionary ORDER BY times DESC LIMIT 1

ibullock
01-20-2012, 03:22 PM
Your code should look something like:




$result = mysql_query("SELECT word,hash FROM dictionary ORDER BY times DESC LIMIT 1");

while($row = mysql_fetch_array($result))
{
echo $row['word'] . " " . $row['hash'];
}



That should select 1 row, with the highest value for "times". If you wanted a list ordered by "times" you can remove the "LIMIT 1" from the end of the statement.