| | |
Help with mysql query + Count
Please support our MySQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Nov 2007
Posts: 53
Reputation:
Solved Threads: 0
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'.
Thanks again to anyone who can help me.
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'.
MySQL Syntax (Toggle Plain Text)
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.
•
•
Join Date: Aug 2008
Posts: 1,160
Reputation:
Solved Threads: 137
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
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
MySQL Syntax (Toggle Plain Text)
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
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
•
•
Join Date: Nov 2007
Posts: 53
Reputation:
Solved Threads: 0
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!
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!
•
•
Join Date: Mar 2009
Posts: 36
Reputation:
Solved Threads: 5
Good one, but I wouldn't settle for miscalculations being spat back at me from the system.
I was just working on a similar thing as a reference using subqueries, and this may not be pretty but very fast.
(4x12 ^ 2) * 80 entries gone through in 0.0020 seconds
As you see from my own situation as an example, you should be able to do your count by 0, >0 and NULL posts without allowing for discrepencies in the resultset.
Also, you are not counting the fieldnames, so I recommend not to use count(*) as you then weed the whole set.
Only count your index fields at most to reduce server load.
Not tried this but wouldnt something like this do the same as what you need, without errors in sum total found?
(with reservation for typos -- been working for 29 hours straight)
I was just working on a similar thing as a reference using subqueries, and this may not be pretty but very fast.
(4x12 ^ 2) * 80 entries gone through in 0.0020 seconds
MySQL Syntax (Toggle Plain Text)
SELECT `tid` , `c0` AS xc0, `c1` AS xc1, `tn` , `act` , ( SELECT `cname` FROM `c0` WHERE `c0` = xc0 LIMIT 1 ) AS c0name, ( SELECT `cname` FROM `c1` WHERE `c0` = xc0 AND `c1` = xc1 LIMIT 1 ) AS c1name FROM `training` WHERE `c0` = NULL OR `c0` <3 ORDER BY `xc0` , `xc1` , `tid`
As you see from my own situation as an example, you should be able to do your count by 0, >0 and NULL posts without allowing for discrepencies in the resultset.
Also, you are not counting the fieldnames, so I recommend not to use count(*) as you then weed the whole set.
Only count your index fields at most to reduce server load.
Not tried this but wouldnt something like this do the same as what you need, without errors in sum total found?
(with reservation for typos -- been working for 29 hours straight)
MySQL Syntax (Toggle Plain Text)
-- find top posters SELECT COUNT(`p`) AS `post_count`, (SELECT `user_id` FROM `user` WHERE `posts`.`user_id`=`u`.`user_id`) AS `ugrp` FROM `posts` GROUP BY `ugrp` ORDER BY `post_count` DESC;
•
•
Join Date: Mar 2009
Posts: 36
Reputation:
Solved Threads: 5
Thats odd.
In fact it would return a NULL if the user dont have a post at all since the JOIN would also not find a NULL without specifying a WHERE clause for this fact
Always include a WHERE X <= 0 AND X = NULL
Even in MS SQL it does the same thing.
For that reason I turned the whole thing over and first select users, then call a sub query on the posts/users, and where posts = Null or 0 set to 0 in return.
Thats the theory anyway.
I'll tinkle with a better option when I find a real need for it.
My scenario may not look big but when you consider doing a inner join on a table with 83,500 blobs over 19.1 GB of data in one table, any * in there better have a good reason behind it.
I normally get through 17 records before the server kicks me out again with anything of that type.
FYI my example uses c0, c1 and training for storing courses by main category, sub category and course program. (that should make it more readable)
In fact it would return a NULL if the user dont have a post at all since the JOIN would also not find a NULL without specifying a WHERE clause for this fact
Always include a WHERE X <= 0 AND X = NULL
Even in MS SQL it does the same thing.
For that reason I turned the whole thing over and first select users, then call a sub query on the posts/users, and where posts = Null or 0 set to 0 in return.
Thats the theory anyway.
I'll tinkle with a better option when I find a real need for it.
My scenario may not look big but when you consider doing a inner join on a table with 83,500 blobs over 19.1 GB of data in one table, any * in there better have a good reason behind it.
I normally get through 17 records before the server kicks me out again with anything of that type.
FYI my example uses c0, c1 and training for storing courses by main category, sub category and course program. (that should make it more readable)
Last edited by javmedia; Mar 4th, 2009 at 9:05 am.
•
•
Join Date: Aug 2008
Posts: 1,160
Reputation:
Solved Threads: 137
sorry bout that, was a late night and didn't think twice about it
this will work
this will work
MySQL Syntax (Toggle Plain Text)
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
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
I modified your query a little bit and I don't know why it doesn't return users who have post = 0.
Another query.
Both returns the same result.
Edit: @Dickersonka, Your query works like a charm! Great job!
mysql Syntax (Toggle Plain Text)
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;
mysql Syntax (Toggle Plain Text)
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
Edit: @Dickersonka, Your query works like a charm! Great job!
Last edited by nav33n; Mar 4th, 2009 at 9:43 am.
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
![]() |
Similar Threads
- Displaying date from mysql (PHP)
- Mysql Query (PHP)
- MySQL count(*) (C#)
- MySQL syntax error check for the right syntax to use near 'A (A) )' at line 1 (PHP)
- MySQL nested query / joined query conversion help (MySQL)
- mySQL database searching for registration (MySQL)
Other Threads in the MySQL Forum
- Previous Thread: Row locking ?
- Next Thread: My rs is readOnly Item
| Thread Tools | Search this Thread |
agplv3 alfresco amazon api artisticlicense aws bizspark breathalyzer camparingtocolumns changingprices cmg communityjournalism contentmanagement contractors copyright count crm database design developer development distinct drupal dui ec2 email enterprise eudora facebook form foss gartner gnu government gpl greenit groklaw groupware hiring hyperic images innerjoins insert ip join journalism keyword keywords kickfire laptop law legal license licensing linux maintenance managing mariadb matchingcolumns metron micromanage microsoft microsoftexchange mindtouch montywidenius mozilla multiple music mysql mysqlcolumnupdating mysqldatetimeordermax() mysqlindex mysqlinternalqueries mysqlquery mysqlsearch news open-xchange opendatabasealliance opengovernment opensource oracle penelope php priceupdating query referencedesign resultset saas select sharepoint simpledb sourcecode spotify sql sugarcrm syntax techsupport thunderbird transparency virtualization






