[英]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 我有一个表格,该表格使用ID将产品与关键字相关联
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 因此,我可能有与关键字ID 14关联的产品ID,2、3、4、5和与关键字ID 15关联的产品ID 3、6、9
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)? 我的问题是,如何计算和存储与关键字14关联的ID和与关键字15关联的ID的总数,以此类推(始终添加新关键字)?
MY SQL so far: 到目前为止,我的SQL:
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. 另外,如果有什么术语可以帮助我通过Google搜索进行进一步的研究,那也将是非常受欢迎的。
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. 编辑添加:在上面的示例中,包含关联的表是products_keyword_to_product-我内部连接了另一个表以返回关键字名称。
Edit to add (2): Sorry I was afraid my question would be vague. 编辑添加(2):对不起,我担心我的问题会模糊。 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. 如果我只想使用关键字ID 14来计数所有产品,则可以使用COUNT()AS-如答案中所述,但我还需要使用关键字ID 15和关键字ID 16等来计算产品数量。-希望这样做更有意义。
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;
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 products_keyword_categories
key_cat_id key_cat_name
1 Electronics
2 Toys
3 Software
products_keyword_to_product 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: 编辑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: 编辑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. 每个product_id和key_cat_name组合给出1行。
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. 它对内部结果(如tbl)进行操作,计算key_cat_name和product_id的组合出现在内部的次数。
Check this: Subqueries in MySQL, Part 1 检查这一点: MySQL中的子查询,第1部分
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 选择count(products_keyword_to_product.id),products_keyword_categories.key_cat_name ...其余正确
Updated Answer: 更新的答案:
SELECT COUNT(*), reference_field FROM table WHERE...
HAVING field=value
GROUP BY field
For aggregate conditions you must use HAVING
对于汇总条件,必须使用HAVING
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.