I have a DB structured like this:
CATEGORIES > SUBCATEGORIES > PRODUCTS
In just want show the categories which have an number of products related to it, but I don’t know if my method is the best.
I thinking in putting some SELECT statement inside the first WHERE, but this sounds “unpratical”.
I searched into Google/MySQl docs and nothing helped me.
Example of what I have done with CATEGORIES > SUBCATEGORIES:
SELECT c.*
FROM categories c
WHERE
(
SELECT count(*)
FROM subcategories sc
WHERE sc.id_categories = c.id
) > 2
With this query I can sucessfully see which categories have more than 2 subcategories related to them, I just could do the same, adding the PRODUCTS table to this query. But I almost sure this is going to slow the query. There is some more fast way of doing this type of query?
Thanks!
Something like this, I don’t know your FK relations, so I’m just guessing here.