Jump to content


Photo

Count rows sort of problem


  • Please log in to reply
1 reply to this topic

#1 Zharvek

Zharvek
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 06 May 2006 - 01:28 AM

Well, I know how to count the number of rows a query returns, but Im looking for something a little more in depth and I can't figure it out, nor do I know exactly what to serach for.

Here is what I'm trying to do, I hope somebody has done this before, or can help.

Say I have a bunch of rows in a table, these are news posts by many users. I want to do a query that says how many posts each user has.

My table looks like,

ID | Username | Date | Data

1 | Bob | Today | Testing
2 | Bob | Today | Testing
3 | Bob | Today | Testing
4 | Bob | Today | Testing
5 | Bob | Today | Testing
6 | Tom | Today | Testing
7 | Tom | Today | Testing
8 | Tom | Today | Testing

Thats fake of course, but that is a good example. I want to say: Bob has 5 posts and Tom has 3. I can use a count of rows but that will return 8, and there are more users then those 2 and names cannot be hardcoded in, so a WHERE username= wont work.

I hope somebody can help, thanks for looking! :-)

#2 onepixel

onepixel
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 06 May 2006 - 03:25 AM

$query="Select ID, Username, Count(*) as Posts From Tablename Group By ID";

<a href="http://www.saheltech.com">PHP Programming - WordPress Blog Customization</a>




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users