On Apr 10, 2010, at 10:02 PM, Ashley M. Kirchner wrote:
>
>
> Given a MySQL query like this $q = "select num from table", I get a result
> like this:
>
>
>
> +---+
>
> |num|
>
> +---+
>
> | 1|
>
> | 4|
>
> | 6|
>
> | 2|
>
> | 4|
>
> | 5|
>
> | 3|
>
> | 2|
>
> | 4|
>
> | 2|
>
> | 3|
>
> | 3|
>
> | 2|
>
> | 1|
>
> +---+
>
>
>
> What I want is a listing of numbers sorted by the amount of times they
> appear (so I can take a top 5, or top 10):
>
>
>
> +---+-----+
>
> |num|count|
>
> +---+-----+
>
> | 2| 4|
>
> | 3| 3|
>
> | 4| 3|
>
> | 1| 2|
>
> | 5| 1|
>
> | 6| 1|
>
> +---+-----+
>
>
>
> Is this a query that I can feed to MySQL, or is this something I need to
> sort out in PHP?
>
This query should do it for you:
SELECT
num,
COUNT(num) AS total
FROM
table
GROUP BY
num
ORDER BY
COUNT(num) DESC
LIMIT 10
Take care,
Floyd
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php