Help with mysql query + Count
Hello,
thanks in advance to anyone who can help me with this query.
What I am trying to do is to retreieve a list of all the users from the table 'users' and count how many posts the user has made.
The code below will shows a list of users that have made a post, but I would like is a list of all the users PLUS how many post they have made, even if the number is '0'.
select
count(*) as post_count,
p.post_id,
p.user_id,
u.username
from
posts p
inner join
users u
on
p.user_id = u.user_id
group by username"
Thanks again to anyone who can help me.
dottomm
Junior Poster in Training
89 posts since Nov 2007
Reputation Points: 9
Solved Threads: 5
I will. Thanks. Sub queries look interesting.
dottomm
Junior Poster in Training
89 posts since Nov 2007
Reputation Points: 9
Solved Threads: 5
you are trying to do an inner join on something that isn't there, all users don't have posts
unlike javamedia, i would suggest trying to use inner or outer joins, at least to me it makes life much easier
try this, think it should work
select count(*) as post_count,
u.user_id
from users u
left outer join
posts p
on p.user_id = u.user_id
group by u.user_id
dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
Thank you dickersonka! That is exactly what I was trying to do. I can now see what I was doing wrong with my other query.
One quirk though - In using the query you offered, I get post_count of 1 though there is none by that user. The weird part (and the part that makes it okay for what I'm doing) is it will show 1 post for =0 and it will continue to read 1 until the post_count reaches 2. Then it counts fine. does that make sense? Any idea? Again, thanks a ton!
dottomm
Junior Poster in Training
89 posts since Nov 2007
Reputation Points: 9
Solved Threads: 5
Nice queries.. But still, that wouldn't solve what OP is looking for.. The still doesn't return the count as 0, if a user doesn't have any posts. I created dummy tables to create the same scenario and I am also clueless ! :S
nav33n
Purple hazed!
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
sorry bout that, was a late night and didn't think twice about it
this will work
select
(select count(*) from posts p1 where p1.user_id = u.user_id) as post_count,
u.user_id
from users u
left outer join
posts p
on p.user_id = u.user_id
group by u.user_id
dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
I modified your query a little bit and I don't know why it doesn't return users who have post = 0.
SELECT COUNT(
COMMENT ) AS `post_count` , (
SELECT `user_id`
FROM `users_table` u
WHERE `comments_table`.`user_id` = `u`.`user_id`
) AS `ugrp`
FROM `comments_table`
GROUP BY `ugrp`
ORDER BY `post_count` DESC
LIMIT 0 , 30;
Another query.
SELECT u.user_id, count( * ) AS post_count
FROM users_table u, comments_table p
WHERE u.user_id = p.user_id
GROUP BY u.user_id
LIMIT 0 , 30
Both returns the same result.
Edit: @Dickersonka, Your query works like a charm! Great job! :)
nav33n
Purple hazed!
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
look at my previous post with a subquery just for you javamedia :-)
i'm not against them at all, just meaning don't shy away from the joins, get a understanding of them and use them in combination with subqueries
dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
YAAAA... Knew it would be doable with a subquery...
I just tested this...
select `fn`,`ln`, (select count(`orderid`) from `order` where `client`.`username`=`order`.`username`) AS `xcnt` FROM `client`
WHERE `client`.`active`='1'
GROUP BY `username`;
It returns a count of orders per client and sets the xcnt = 0 where no orders occurred, but shows first name and last name per client
who is active in the system
Subqueries are even better.. :)
select `user_id`, (select count(`comment`) from `comments_table` where `comments_table`.`user_id`=`users_table`.`user_id`) AS `xcnt` FROM `users_table`
GROUP BY `user_id`;
I edited your query and it works ! Eureka ;)
nav33n
Purple hazed!
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
@Dickersonka, I have been advised on so many occasions not to use joins. Joins are relatively slower compared to subqueries. This is what I have read.
nav33n
Purple hazed!
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
So we got two solutions to one problem. Thats what I call a good days work.
(pfiuuh.. its been 36 hours since sleep so I'm outta here, thanks for a nice quiz/problem. It will come in handy for sure.)
Yeah.. 1 solution, good. 2 solutions, better :)
Get some sleep dude! Cheers!
nav33n
Purple hazed!
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
Thank you Dickersonka. Works like a charm!!
Thank yo to all of you for your input and most of all your expertise!
dottomm
Junior Poster in Training
89 posts since Nov 2007
Reputation Points: 9
Solved Threads: 5
dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143