Advanced Search

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,430
    Thanks
    37
    Thanked 84 Times in 83 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?
    keebs - keyboard1333 [at] gmail [dot] com
    My Website | Anime Views Forums

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,202
    Thanks
    63
    Thanked 454 Times in 442 Posts
    Blog Entries
    7

    Default

    Code:
    SELECT * FROM dictionary ORDER BY times DESC LIMIT 1
    Last edited by traq; 01-20-2012 at 03:23 PM.
    Adrian ~ facebook | gist/github

    ['66.215.156.37','208.75.149.97'] // ip,ip array!
    "Take that sticker *off* your hat; you look stupid" --Wil Wheaton

  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
  •