Username_null
Username_null

Reputation: 1315

MySQL How do I count the number of occurences of each ID?

It may be difficult to explain what I am after, apologies if the question is vague.

I have a table which associates products with keywords using IDs

So I may have product IDs, 2,3,4,5 associated with Keyword id 14 and product IDs 3,6,9 associated with Keyword id 15

My question is How do I count and store the total for those IDs associated with Keyword 14 and for those IDs associated with Keyword 15 and so on (New Keywords added all the time)?

MY SQL so far:

select products_keyword_categories.key_cat_name 
from products_keyword_to_product 
inner join products_keyword_categories 
on products_keyword_categories.key_cat_id = products_keyword_to_product.key_cat_id 
group by products_keyword_categories.key_cat_name

Many thanks in advance for any advice. Also, if there is any terminology that will aid me in further research via a Google search that would also be most welcome.

Edit to add: In the example above the table containing the associations is products_keyword_to_product - I inner join the other table to return the Keyword name.

Edit to add (2): Sorry I was afraid my question would be vague. If I wanted to just count all the products using keyword ID 14 I would use COUNT() AS - As mentioned in the answers but I also need to count the number of products using Keyword ID 15 and Keyword ID 16 etc. - Hope that makes more sense.

Upvotes: 0

Views: 212

Answers (4)

Leandro Bardelli
Leandro Bardelli

Reputation: 11578

Updated Answer:

SELECT COUNT(*), reference_field FROM table WHERE...
HAVING field=value
GROUP BY field

For aggregate conditions you must use HAVING

Upvotes: 0

Nathan
Nathan

Reputation: 2775

select cat.key_cat_name, count(*) from
products_keyword_categories cat inner join products_keyword_to_product prod
on prod.key_cat_id=cat.key_cat_id
group by cat.key_cat_name

Edit:

select cat.key_cat_name, prod_assoc.product_id, count(*) from
products_keyword_categories cat inner join products_keyword_to_product prod_assoc
on prod_assoc.key_cat_id=cat.key_cat_id
group by cat.key_cat_name,prod_assoc.product_id

Assuming your tables structure is like this:

products_keyword_categories

key_cat_id key_cat_name

1          Electronics

2          Toys

3          Software

products_keyword_to_product

key_cat_id  product_id

1           1 

2           1

3           2

1           2

products

product_id name

1          Product A

2          Robot 

Edit 2:

Try this

SELECT key_cat_name, product_id, COUNT(*)
FROM
(select cat.key_cat_name, prod_assoc.product_id from
products_keyword_categories cat inner join products_keyword_to_product prod_assoc
on prod_assoc.key_cat_id=cat.key_cat_id) as tbl
GROUP BY key_cat_name, product_id

Edit 3:

The query above is made of 2 parts:

The inner part:

(select cat.key_cat_name, prod_assoc.product_id from
products_keyword_categories cat inner join products_keyword_to_product prod_assoc
on prod_assoc.key_cat_id=cat.key_cat_id)

Which gives 1 row per combination of product_id and key_cat_name.

The outer part:

SELECT key_cat_name, product_id, COUNT(*)
FROM (...) as tbl
GROUP BY key_cat_name, product_id

Which operates on the results of the inner part (as tbl), counting how many times a combination of key_cat_name and product_id appears on the inner part.

Check this: Subqueries in MySQL, Part 1

Upvotes: 1

Gayathri
Gayathri

Reputation: 136

select key_cat_name ,count(*) 
from products_keyword_categories pkc 
    inner join products_keyword_to_product ptk on pkc.id=ptk.key_id 
group by id;

Upvotes: 3

Sérgio Abreu
Sérgio Abreu

Reputation: 177

You are almost there, you just need to add the following:

select count(products_keyword_to_product.id), products_keyword_categories.key_cat_name ... the rest is correct

Upvotes: 0

Related Questions