Results 1 to 3 of 3

Thread: select from database where highest value

  1. #1
    Join Date
    Mar 2011
    Location
    N 11 19' 0.0012 E 142 15' 0
    Posts
    1,531
    Thanks
    41
    Thanked 89 Times in 88 Posts
    Blog Entries
    3

    Default select from database where highest value

    Hello everyone,

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

    The insert code looks like this

    PHP Code:
    $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?

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Code:
    SELECT * FROM dictionary ORDER BY times DESC LIMIT 1
    Last edited by traq; 01-20-2012 at 04:23 PM.

  3. #3
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Your code should look something like:

    PHP Code:

    $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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •