TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Display Modes
Old 07-24-2008, 02:50 AM   #1 (permalink)
The Gregarious
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Cana'derr
Posts: 653
Thanks: 24
delayedinsanity is on a distinguished road
Default MySQL join total

How would I go about forming this into one SQL query?

I have two tables, one with my news in them, one with the comments for each news article. I want to select ALL the news items, and return another column for each row with a total count of how many rows in the comments table there are which match the news id.

So basically what I want is something akin to this:

Code:
SELECT news.id, news.uid, news.cid, news.headline, news.body, count(SELECT id FROM news_comments WHERE news_comments.nid = news.id) AS total FROM news;
Except I don't know how to phrase it in proper SQL.
-m
delayedinsanity is offline  
Reply With Quote
Old 07-24-2008, 04:13 AM   #2 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Location: On your Hard Drive, hiding like a Virus
Posts: 818
Thanks: 163
Orc is on a distinguished road
Default

Quote:
Originally Posted by delayedinsanity View Post
How would I go about forming this into one SQL query?

I have two tables, one with my news in them, one with the comments for each news article. I want to select ALL the news items, and return another column for each row with a total count of how many rows in the comments table there are which match the news id.

So basically what I want is something akin to this:

Code:
SELECT news.id, news.uid, news.cid, news.headline, news.body, count(SELECT id FROM news_comments WHERE news_comments.nid = news.id) AS total FROM news;
Except I don't know how to phrase it in proper SQL.
-m
You could do a left join as such

sql Code:
SELECT news.id, news.uid, news.cid, news.headline, news.body, count(DISTINCT news_comments.id) AS comment_count FROM news
LEFT JOIN news_comments AS news_comments ON news_comments.nid=news.id

GROUP BY news.id

Basically what this does is grab all your columns from news table, and then does a left join clause to news_comments and assigns the nid to the news id, then you could count all the comment ids dinstinctly and then rename it to comment_count, and by using group by you could still keep it where it lists all the news by how many id's there instead of the comments id's. DISTINCT is just used so it's by itself, so if you add any more count()'s you could add DISTINCT and they would have their own counts. Sorry if this is a bad description, but I can't sum it up very well. Just take it to some knowledge

If you have multiple count()'s in select without DISTINCT, they could add onto the other count clauses, thus say if you have 2 news rows, and 1 comments rows, then it would return the comments rows as 2 instead of 1 when you really have just one comment, don't understand why to be honest.
__________________
Wax on, Wax off
Orc is offline  
Reply With Quote
The Following 2 Users Say Thank You to Orc For This Useful Post:
delayedinsanity (07-24-2008), Matt (07-24-2008)
Old 07-24-2008, 04:25 AM   #3 (permalink)
The Gregarious
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Cana'derr
Posts: 653
Thanks: 24
delayedinsanity is on a distinguished road
Default

Nice! I've got a lot to learn when it comes to SQL joins, I just know the very basic basics at this point... probably the next thing I should endeavour to figure out.

That query does exactly what I needed though, so again, thank you.
-m
delayedinsanity is offline  
Reply With Quote
Old 07-24-2008, 04:53 AM   #4 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Location: On your Hard Drive, hiding like a Virus
Posts: 818
Thanks: 163
Orc is on a distinguished road
Default

Quote:
Originally Posted by delayedinsanity View Post
Nice! I've got a lot to learn when it comes to SQL joins, I just know the very basic basics at this point... probably the next thing I should endeavour to figure out.

That query does exactly what I needed though, so again, thank you.
-m
No problem, I had to do the same thing a few days ago. xD
__________________
Wax on, Wax off
Orc is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

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


All times are GMT. The time now is 05:23 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0