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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

agregate Count return all rows, Left Jin 1

Status
Not open for further replies.

SPrelewicz

Programmer
Jul 16, 2001
124
US
I have 2 tables on a lyric discussion site

Titles
title_id
title_name
title_lyricist
title_lyrics
title_artist
title_entered

And POSTS
post_id
title_id
post_author
post_text

I want to be able to list all the titles, and count the # of posts for each title. The problem is some titles have zero posts so my query ignores thos titles, butI syill want them returnd where Ill add a ')' for count.

This is the best i could come up with, but still only those that have posts are returned. I still want the titles that don't have a match in the posts table to be returned with a total_count of zero.

Any any help on this ASAP would be much appreciated.


<a href=' Roman lyric meanings</a>
 
this is a classic situation for a left outer join
Code:
select title_id
     , title_name       
     , title_lyricist       
     , title_lyrics       
     , title_artist       
     , title_entered  
     , count(post_id) as posts
  from Titles
left outer
  join POSTS
    on Titles.title_id
     = POSTS.title_id    
group
    by title_id
     , title_name       
     , title_lyricist       
     , title_lyrics       
     , title_artist       
     , title_entered

rudy
SQL Consulting
 
Like magic. it's OUTER joins I just cant seem to grasp, but hopefully this example will engrave in my memory.

Thanks alot!

And for posting on 2 bords ;)

Scott

 
Okay, this all worked great except for the title_id. the table returns 2 title_id fields, 1 from each table. In my Perl, when I process each record, it gives me the post title_id which is null. How would I structure the query so I gotthe title_id from titles, or only the title_id from titles was returned?

Thanks again.

About me at Flying Roman.com lyric meaning and discussion
Prelewic, Renassance Man ;)
 
oh yeah, i shoulda caught that, sorry

change the first line to
Code:
select Titles.title_id

rudy
SQL Consulting
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top