#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 1999
    Posts
    119
    Rep Power
    16
    I need to find the largest value of a field within a given table. For example, I have an AUTO_INCREMENT column, and I need to find the largest value... How do you do that? sorry if this is an ignorant question...
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Location
    London, England
    Posts
    251
    Rep Power
    15
    Just select the field, sort it and take the first row at the top.

    SELECT field
    FROM table
    ORDER BY field DESC
    LIMIT 1
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    13
    Rep Power
    0
    I have a question: which way is better to find the max: using limit and order by, or using MAX?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 1999
    Posts
    119
    Rep Power
    16
    *Is* there a MAX function?
  8. #5
  9. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Location
    Niagara Falls (On the wrong side of the gorge)
    Posts
    3,237
    Rep Power
    19
    Use limit and order_by. max() should be used if you want to return several max values. For example, let's say you have a table with products that has the product_id, a category_id and price. You need to find the most expensive product in each category. You could do this:

    select product_id,category_id,max(price) as m_price from products group by category_id order by category_id;

    This would return the most expensive product for each category.

Similar Threads

  1. Find a max value in mysql with php ?
    By gjs in forum PHP Development
    Replies: 3
    Last Post: October 20th, 2008, 07:02 AM
  2. Help with MySQL COUNT function
    By Jaberwooky in forum MySQL Help
    Replies: 1
    Last Post: September 3rd, 2003, 01:17 AM
  3. Access to MySQL using C in QNX
    By Ramli_Xu in forum MySQL Help
    Replies: 0
    Last Post: August 17th, 2003, 09:26 PM
  4. Can't get WYSIWYG editor to function right
    By Mobius Man in forum HTML Programming
    Replies: 5
    Last Post: July 24th, 2003, 11:51 PM
  5. max() function for MySQL
    By brooky in forum PHP Development
    Replies: 2
    Last Post: March 13th, 2002, 08:22 AM

IMN logo majestic logo threadwatch logo seochat tools logo