Johnathan Au
Johnathan Au

Reputation: 5362

How do I find the maximum value of a field in MYSQL?

I have a table called courses. The fields are:

id, category, sortorder, fullname, etc.....

What I am trying to do is given the category I want to find the maximum sortorder number in that category and then increment that number and use this new number for my new row.

For example: I have category = 30 and it will return the numbers 1 to 10. I want to retrieve 10 because it is the highest number but I don't know what the highest number will actually be in each query. After I get the value 10 I will add 1 to it and then use it for my new course.

How should I do this ?

Thanks!

Upvotes: 0

Views: 477

Answers (3)

heyanshukla
heyanshukla

Reputation: 669

Might this help

    SELECT MAX(sortorder)
    FROM table_name
    WHERE category = 10

Upvotes: 1

Nick
Nick

Reputation: 6346

Take a look at the max function in MYSQL.

Not sure what your column is called, but something like this:

SELECT MAX(`sortorder`) AS `max` FROM `table`

Upvotes: 1

Rene Pot
Rene Pot

Reputation: 24815

You can use the MAX() function of MySQL.

So do something like:

SELECT MAX(sortorder) as maximum FROM courses WHERE category = 10

Upvotes: 4

Related Questions