Hi Dave,

The easiest, most direct, ANSI - SQL way to do what you ask is:

SELECT count(*) as num_posts, Admin_id
  FROM <<your_table_Name>>
  WHERE <<optional but you could narrow the results here>>
  GROUP BY Admin_id
  ORDER BY Admin_id

This will give you:

num_posts  Admin_id
---------  --------
  3         1
  4         2
  5         3

And naturally you can do whatever you like with these values in your PHP
code.  The only potential "got ya" is if you had an Admin_id 4 who had no
records you would not get 0, 4 you would not get a row for 4 at all.  There
is a way around.  Ask me if you need it.

Also GROUP BY has it's own kind of WHERE clause called HAVING.  The WHERTE
clause operates on the raw data (like any other query) but HAVING works on
the aggregate functions (like count, min, max, ave) so in this example we
could add this line right after the GROUP BY:
  HAVING count(*) > 4
This would make the query only return the 5, 3 row.

Good Luck,
Frank

On 5/1/02 12:24 PM, "[EMAIL PROTECTED]"
<[EMAIL PROTECTED]> wrote:

> From: "Dave Carrera" <[EMAIL PROTECTED]>
> Date: Wed, 1 May 2002 13:02:51 +0100
> To: <[EMAIL PROTECTED]>
> Subject: Count and group ?
> 
> Hi All
> 
> 
> 
> I have added a row to my table which inputs which admin user has amended
> a record in a table on my db.
> 
> 
> 
> I have this working but what I would like to do is count how many
> instances of the admin id I have stored.
> 
> 
> 
> So if my list looks like this..
> 
> 
> 
> Admin_id
> 
> 1
> 
> 2
> 
> 2
> 
> 2
> 
> 3
> 
> 3
> 
> 3
> 
> 3
> 
> 3
> 
> 2
> 
> 1
> 
> 1
> 
> Then the result I would like to display is this..
> 
> 
> 
> Admin (1) = 3 posts
> 
> Admin (2) = 4 posts
> 
> Admin (3) = 5 posts
> 
> 
> 
> Basically displaying the total post each admin has made
> 
> 
> 
> Any help or guidance is very appreciated and thank you in advance
> 
> 
> 
> Dave Carrera
> 
> Php Developer
> 
> http://davecarrera.freelancers.net
> 
> http://www.davecarrera.com
> 
> 


-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to