Jump to content

Recommended Posts

I have 3 tables ;

Category

Subcategory

Posts

 

Posts are under subcategories which are under categories.

 

Example :

Category :

id | category

7  | Vehicles

8 | Electronics

 

Subcategory :

id | subcat | catid

3  | Car      | 7

4  | Phone | 8

 

Posts :

id | topic | subcatid

1 | BMW |  3

2 | Audi  | 3

 

And I want a result like

 

Vehicles

- Car ( 2 Posts )

Electronics

- Phone ( 7 Posts )

 

I could not write a query for this, The query I wrote just returns 1 row.

What can be the query of this ?

Link to comment
https://forums.phpfreaks.com/topic/229519-count-usage-3-tables/
Share on other sites

I know,

my query is as follows :

$query = "

SELECT c.*,sc.*,p.*, COUNT(p.*)

FROM category c,subcategory cs,posts p

WHERE

p.subcatid = sc.id AND

sc.id = c.id"

 

and this does not work, just returns the first row.

Whats wrong with my query ?

you're using select * three times (once is bad enough, but three times in a single query is inexcusable)  :P

you are aliasing table names for no good reason

you don't have a single JOIN in your FROM clause even though you are supposed to have two of them

 

other than that, it looks ok  ;D

you're using select * three times (once is bad enough, but three times in a single query is inexcusable)  :P

you are aliasing table names for no good reason

you don't have a single JOIN in your FROM clause even though you are supposed to have two of them

 

other than that, it looks ok  ;D

I m really not good in this query things.

I would be glad, if you could explain me what you mean ?

"you are aliasing table names for no good reason" ?

"you don't have a single JOIN in your FROM" ?

"you are aliasing table names for no good reason" ?

 

You're using aliases when you don't need to

 

"you don't have a single JOIN in your FROM" ?

 

You don't have any joins in there

 

Try this sql:

SELECT COUNT(id) AS CNT 
FROM Posts
INNER JOIN Subcategory ON Posts.subcatid = Subcategory.id
INNER JOIN Category ON Subcategory.catid = Category.id
WHERE Category.id=$your_desired_category_id

 

Not tested but should get you started

You don't have any joins in there

 

Try this sql:

SELECT COUNT(id) AS CNT 
FROM Posts
INNER JOIN Subcategory ON Posts.subcatid = Subcategory.id
INNER JOIN Category ON Subcategory.catid = Category.id
WHERE Category.id=$your_desired_category_id

 

Not tested but should get you started

Actually I want to show the all categories and sub categories and the count of posts like :

 

Vehicles

- Car ( 2 Posts )

Electronics

- Phone ( 7 Posts )

 

I need to SELECT more I guess :S ?

and there is one thing that I did not understand,

what is this CNT for ?

"SELECT COUNT(id) AS CNT " ?

CNT stood for count, which was the one alias you would use to echo it later, like:

while ($row = mysql_fetch_assoc($your_result)) { echo "$row[CNT]"; }

 

I said it would get you started. Just showing an example of the type of join you need. If you ran it with the category you wanted it would return the count. Loop it and you might be close.

 

Try and then show us your code

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.