Go Back   Forum Care Forums > Development Reference Area > MySQL Discussion

Reply
 
LinkBack Thread Tools Display Modes
an advanced query for a newbie
Old
  (#1)
Guest
Guest
 
Posts: n/a
Default an advanced query for a newbie - 06-04-2007, 07:58 AM

Hi,
I'm developing a simple weblog application. What i want to do is to
list all users with their id, name, total posts and total comments
infos like this:

id name total_posts total_comments
1 abcd 12 25
2 weav 0 3
....


My mysql scheme is:
--------
users
--------
id, name

--------
posts
--------
id, user_id

---------------
comments
---------------
id, post_id, user_id

I've tried some queries but couldn't make it. Please help me to
achieve my goal.
Thanks.

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Re: an advanced query for a newbie
Old
  (#2)
Guest
Guest
 
Posts: n/a
Default Re: an advanced query for a newbie - 06-04-2007, 07:58 AM

On 20 Mayýs, 01:52, "sempst...@gmail.com" <sempst...@gmail.com> wrote:
> Hi,
> I'm developing a simple weblog application. What i want to do is to
> list all users with their id, name, total posts and total comments
> infos like this:
>
> id name total_posts total_comments
> 1 abcd 12 25
> 2 weav 0 3
> ...
>
> My mysql scheme is:
> --------
> users
> --------
> id, name
>
> --------
> posts
> --------
> id, user_id
>
> ---------------
> comments
> ---------------
> id, post_id, user_id
>
> I've tried some queries but couldn't make it. Please help me to
> achieve my goal.
> Thanks.


I did it:

SELECT u.id, u.username, count(p.id) as total_posts, count(c.id) as
total_comments FROM users u LEFT JOIN posts p ON p.user_id = u.id LEFT
JOIN comments c ON c.user_id = u.id GROUP BY u.id;

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Contact Us - Forum Care Forums - Archive - Top