Max value

A

Anonymous

Guest
Is there a function or something that returns all the numerical values in a column of a mysql table as an array?

What i want to determine is the highest value in column, which I know i can do with the max() function if I can get the values into an array...

There are plenty of functions for returning a row, but i cant find any for a column..

help???!!!!

TIA
 
Once again, I recommend the MySQL & PHP Coding forum.

Anyway, a good rule of thumb for dealing with databases and PHP is that if you can get MySQL to do it for you, go for it. The MySQL manual (it'll do you good to give the docs at least a cursory search before asking for help) has a page devoted exactly to this question. See pages 3.5.1 and 3.5.2.

Basically, if you want to return only the maximum value in a column, use MySQL's built-in MAX() function:

SELECT MAX(column_name) FROM table1;

If you want to return the whole row, just use ORDER BY the field you want in descending order and LIMIT the result to one row:

SELECT * FROM table1 ORDER BY column_name DESC LIMIT 1;

By the way, as a general rule, unless you're going to need the value from every single column, indicate only the columns that you need (e.g. "SELECT column1, column2 FROM ..."), rather than using the asterisk ("SELECT * FROM ...").
 
Back
Top