Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2006
    Posts
    7

    Unanswered: MYSQL MAX function

    Any help would be greatly appreciated --- this forum's search function wasn't providing any results for this. I have table with thousands of rows that contains data like this:

    OrderNum
    ------------

    1
    29
    703
    34
    ...
    1400
    1552
    1402
    1410
    1377


    I'm trying to find the MAX value in the OrderNum column. But, the code below keeps showing 99 as the result, when 1552 is the value that I'm trying to achieve.

    Does anyone know what I'm doing wrong with this code? Thank you very much.


    $table_name="sales";
    $sql = "SELECT max(OrderNum) FROM $table_name";
    $result = mysql_query($sql,$connection) or die(mysql_error());
    $row = mysql_fetch_row($result);
    echo $row[0];

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    OrderNum is most probably a varchar, right?

    why?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    Quote Originally Posted by r937
    OrderNum is most probably a varchar, right?

    why?
    ...good point, and well made...
    I'd rather be riding on the Tiger 800 or the Norton

  4. #4
    Join Date
    Feb 2006
    Posts
    7
    Yes, my OrderNum field was, indeed, a varchar... I converted it to MEDIUMINT, and it works now. Thank you very much for your help!
    Last edited by stevem; 03-03-06 at 00:18.

Posting Permissions

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