Re: counting of values in one field possible with one sql statement?
| From: | Frank Flynn | Date: | Fri, 08 Mar 2002 00:56:51 +0000 |
| Subject: | Re: counting of values in one field possible with one sql statement? | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-17560@lists.php.net to get a copy of this message | ||
Yes, in fact there is a faster way, GROUP BY
SELECT COUNT(*) AS c, age
FROM $T5
GROUP BY age
This will give you two columns 'c' and 'age' which will be the count (the
number of) and each age. So from your example (0,3,5,5,2,3) we'd get:
c age
1 0
2 3
2 5
1 2
That's easier, right?
Good Luck,
Frank
On 3/7/02 4:40 PM, "php-db-digest-help@lists.php.net"
<php-db-digest-help@lists.php.net> wrote:
> From: "Andy" <news.letters@gmx.de>
> Reply-To: "Andy" <news.letters@gmx.de>
> Date: Fri, 8 Mar 2002 01:06:09 +0100
> To: php-db@lists.php.net
> Subject: counting of values in one field possible with one sql statement?
>
> Hi there,
>
> I am wondering if it would be possible to get the amount of different values
> in one sql command.
> E.g.:
> 0,3,5,5,2,3
>
> Sql querry should return how often 3 occurs. Which would be 2. And as well
> the other values... 1 for agestructure 2 becuase it occures only once.
>
> I wrote following querry
>
> $stmt="
> SELECT COUNT(*) AS c
> FROM $T5
> WHERE age = 4
> ";
> $row = db_get_row2($stmt);
> $age_4 = $row->c;
>
> This returns only the value for 4. To get all values (10 different ones) I
> would have to make 10 querrys, right?
>
> Is there a faster way?
>
> Thanx for any help,
>
> Andy
--
Frank Flynn
Poet, Artist & Mystic