Connecting Tech Pros Worldwide Forums | Help | Site Map

using SELECT to just find row with maximum value

mr_burns
Guest
 
Posts: n/a
#1: Jul 17 '05
hi,

how do i do a SELECT statment that will just query a table to find the
row with the maximum of a declared field. for example, if i have a
table of customers and i would like to get the value of the last
customer to register so the SELECT statement would return only one
row, how would i do this?

also, the plan i have is to use a while loop (coz i dont know any
other) to get the colum value of each row:

$select = "SELECT ...
$result = mysql_query($select, $conn); //$conn will be predefined in
the code
while ($row = mysql_fetch_array($result)) {
$date_added = $row['date_added'];
}

the while loop makes sense when outputting many rows but to get the
value of one column in one row, i dont know. it just seems a little
inefficient to use a while loop when i know there is only one row
returned (or will know), is this how all rows from a select statement
are queried or is there a more efficient way of doing this for this
situation? cheers

burnsy

Chris Hope
Guest
 
Posts: n/a
#2: Jul 17 '05

re: using SELECT to just find row with maximum value


mr_burns wrote:
[color=blue]
> how do i do a SELECT statment that will just query a table to find the
> row with the maximum of a declared field. for example, if i have a
> table of customers and i would like to get the value of the last
> customer to register so the SELECT statement would return only one
> row, how would i do this?[/color]

[snip]

SELECT MAX(customer_id) FROM customers

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Gordon Burditt
Guest
 
Posts: n/a
#3: Jul 17 '05

re: using SELECT to just find row with maximum value


>[color=blue]
>how do i do a SELECT statment that will just query a table to find the
>row with the maximum of a declared field. for example, if i have a
>table of customers and i would like to get the value of the last
>customer to register so the SELECT statement would return only one
>row, how would i do this?[/color]

SELECT max(balance) from customer_accounts;

*DO NOT* however, count on that info being up to date by the time
you use it. One of the worst things you can do is get the id of
the last customer to register, and add 1, and assume that will be
the id of the NEXT customer to register. You can easily have several
pages determine that ID 666 is the last customer to register, then
they all try to grab id 667, and only one wins.

MySQL has features like auto_increment columns to deal with assigning
ID numbers in a single query. Associated with that is the MySQL
function last_insert_id() which returns the ID number from the last
auto_increment *YOU* added (it's based on the connection, so the
answer will not change if someone adds another record between the
one you added and your query to get the last_insert_id). Even with
persistent connections, only one page can be using a connection at
one time.

Gordon L. Burditt
Closed Thread