Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help me with a Query

Status
Not open for further replies.

cLFlaVA

Programmer
Jun 14, 2004
6,450
US
Hi all. My brain hurts.
Here is my data:

[tt]news table
-----------
news_id
news_title
news_body

comments table
---------------
comment_id
news_id
comment_text[/tt]

My desired result is a recordset that returns news_id, news_title, news_body, and number of comments for each news item.

I'm sure this is easy, but my brain is fried from a busy week.

Thanks a lot for your help.

*cLFlaVA
----------------------------
[tt]Sigs cause cancer.[/tt]
 
Got it!

Code:
  SELECT a.news_id
       , a.news_title
       , a.news_body
       , COUNT(b.comment_id) as comments
    FROM news a LEFT JOIN comments b ON
         a.news_id = b.comment_news_id
GROUP BY a.news_id

*cLFlaVA
----------------------------
[tt]Sigs cause cancer.[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top