bphein1980
11-16-2006, 02:05 AM
I have 2 tables that I need to Inner Join and then count how many duplicates of a record I have.
Here is the set up
Table 1
Username / Post
------------------
user1 / POST1
user1 / POST2
user4 / POST3
user5 / POST4
user4 / POST5
Table 2
Username / Reply
------------------
user2 / REPLY1
user2 / REPLY2
user3 / REPLY3
user1 / REPLY4
user1 / REPLY5
I am trying to count how many posts and replies each username has. Such as:
user1 = 4
user2 = 2
user3 = 1
etc...
I am stuck on this. I have researched using google and such, found some example, but cant seem to get them implemented correctly. Here is was I have so far, but it is coming out totally wrong. There are at least 3 usernames listed between the 2 tables and it is only displaying 1 with a wrong count. What am I doing wrong?
$query = mysql_query("
SELECT forum_reply.username, count(forum_reply.username)
AS userc FROM forum_reply
INNER JOIN forum_posts ON forum_reply.username = forum_posts.username
GROUP BY forum_reply.username");
while($r = mysql_fetch_array($query)){
echo $r['username']." ".$r['userc']."<br/>";
}
Here is the set up
Table 1
Username / Post
------------------
user1 / POST1
user1 / POST2
user4 / POST3
user5 / POST4
user4 / POST5
Table 2
Username / Reply
------------------
user2 / REPLY1
user2 / REPLY2
user3 / REPLY3
user1 / REPLY4
user1 / REPLY5
I am trying to count how many posts and replies each username has. Such as:
user1 = 4
user2 = 2
user3 = 1
etc...
I am stuck on this. I have researched using google and such, found some example, but cant seem to get them implemented correctly. Here is was I have so far, but it is coming out totally wrong. There are at least 3 usernames listed between the 2 tables and it is only displaying 1 with a wrong count. What am I doing wrong?
$query = mysql_query("
SELECT forum_reply.username, count(forum_reply.username)
AS userc FROM forum_reply
INNER JOIN forum_posts ON forum_reply.username = forum_posts.username
GROUP BY forum_reply.username");
while($r = mysql_fetch_array($query)){
echo $r['username']." ".$r['userc']."<br/>";
}