Jump to content

Recommended Posts

Not really sure on how to do this as i'm not too great with mysql.

 

I have 2 tables categories and news.

news has a unique id and a category id

categories has the catagory id and the name to be used on it.

 

Im trying to make some stats that lists all the category names and the number of times it has been used in the news table

 

This is my latest attempt at it.

 

SELECT COUNT( * ) AS rows, news.category, categories.catname FROM news, categories GROUP BY category ORDER BY category WHERE catid=category

Link to comment
https://forums.phpfreaks.com/topic/193941-using-a-count-when-using-2-tables/
Share on other sites

SELECT c.*, COUNT(n.category) AS newsCount FROM categories AS c LEFT JOIN news AS n ON (n.category = c.id) ORDER BY c.catname ASC

 

Something like that.

It's a bit confusing to create it for you since I don't know the fields and they seem quite similar to eachother ;)

fantastic!

 

I've managed to get it right thanks to your code which wasnt too far off what I needed. just had to add in the group by and change the id to catid

 

the working code being:

SELECT c.*, COUNT(n.category) AS newsCount FROM categories AS c LEFT JOIN news AS n ON (n.category = c.catid) GROUP BY c.catname ORDER BY c.catname ASC

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.