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

JOINS - and a Record Count 1

Status
Not open for further replies.

TamedTech

IS-IT--Management
May 3, 2005
998
GB
Hello Guys,

I'm having issues writing a query for this feature, i'm sure its not complicated but I could use a little assistance.

Basicly i have a list of Articles, these are all sorted in my table called 'tblArticles' ... each article belongs to a catagory, the catagory details are stored in another table called 'tblArticlesCatagories' ... in the Articles table there is a field called 'Article_catagory' which refers to the Primary Key of the 'tblArticlesCatagories' table.

Now what I want to do is display a list of my catagories along with a description for each one, and next to the catagory name have a record count of articles that belong to that Catagory, so it would appear like so.

Catagory 1 (5 Articles)
-----------------------
This Catagory contains lots of informaiton on blah blah blah...

Catagory 2 (24 Articles)
-----------------------
This Catagory contains lots of informaiton on blah blah blah...


You see what i'm trying to achieve? I've played around with a few different JOINS but keep getting odd results, for instants if catagory 1 contains 3 articles then it will display the title and description for catagory 1 three times!

Any help you can offer would be greatly appreciated.

Rob
 
Code:
select tblArticlesCatagories.descr
     , count(tblArticles.Article_catagory)
            as article_count
  from tblArticlesCatagories
left outer
  join tblArticles
    on tblArticles.Article_catagory
     = tblArticlesCatagories.id
group
    by tblArticlesCatagories.descr

r937.com | rudy.ca
 
Thanks for that Rudy ... LEFT OUTER JOIN it was, i just ammended the field names so it looks as follows.

Code:
	SELECT tblArticlesCatagories.catagory_id, tblArticlesCatagories.catagory_name, tblArticlesCatagories.catagory_description, count(tblArticles.Article_ctg) AS article_count
	FROM tblArticlesCatagories
	LEFT OUTER JOIN tblArticles ON tblArticles.Article_ctg = tblArticlesCatagories.catagory_id
	GROUP BY tblArticlesCatagories.catagory_description

And it working perfectly, thanks for your help,

Rob
 
that gots to be mysql, because that's the only database i know of that will allow you to write an invalid GROUP BY

if you have this --

SELECT
tblArticlesCatagories.catagory_id
, tblArticlesCatagories.catagory_name
, tblArticlesCatagories.catagory_description
, count(tblArticles.Article_ctg)

then you need to use this --

GROUP BY
tblArticlesCatagories.catagory_id
, tblArticlesCatagories.catagory_name
, tblArticlesCatagories.catagory_description

:)

r937.com | rudy.ca
 
lol .. thanks for that Rudy,

As you spotted its the fabled old MySQL ... it was working without any beef even with the dodgy GROUP BY statement, but i've ammended anyway.

Thanks again Rudy,

Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top