Max value

General discussions related to php

Moderators: macek, egami, gesf

User avatar
frogrocker
New php-forum User
New php-forum User
Posts: 98
Joined: Mon Jul 07, 2003 10:30 pm
Location: Manchester
Contact:

Max value

Postby frogrocker » Tue Jul 08, 2003 7:58 am

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

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Postby swirlee » Tue Jul 08, 2003 11:33 am

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 ...").


Return to “PHP General”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest