Re: getting only the largest or smallest values in a selectstatement
| From: | Monte Ohrt | Date: | Mon, 09 Oct 2000 19:41:35 +0000 |
| Subject: | Re: getting only the largest or smallest values in a selectstatement | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-19234@lists.php.net to get a copy of this message | ||
if you only need the amount and not the id:
select min(amount) minimum, max(amount) maximum from table limit 1;
if you need both the id and the amount, run two queries:
select id, amount from table order by amount limit 1;
select id, amount from table order by amount desc limit 1;
Another solution is to select all the rows ordered by amount, and grab
the first and last elements from the arrays in PHP. Not as efficient as
the other two, but if you're grabbing all the data for other purposes
anyways... it all depends on your application and which solution best
suits your needs.
Monte
Dave Gardner wrote:
>
> How can I get the largest (or smallest) value of a column in multiple
> records, culled from a select statement? Let's say I did a 'select id,
> amount from purchases' and got the following:
>
> id amount
> -- -------
> 1 488
> 2 1256
> 3 188
> 4 97
> 5 166
>
> Is it possible to modify the query itself to select only the largest or
> smallest value in 'amount' as displayed here? Or is there something I need
> to do in PHP to figure that out?
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
--
Monte Ohrt <monte@ispi.net>
http://www.ispi.net/