[英]MySQL : Multiple counts in one query
I have a table of products. 我有一张产品表。 Every product has a company and a worker of that company that produces it. 每个产品都有一个公司和一个生产它的公司的工人。
Something like this: 像这样:
product | company | worker
--------------------------
1 2 John
2 2 Mike
3 2 Jim
4 3 Mark
5 3 Fred
6 2 John
etc...
Is it possible to run one query by company, and count all products by specific worker that works for selected company. 是否可以按公司运行一个查询,并按为所选公司工作的特定工人对所有产品进行计数。
For example: 例如:
SELECT (count how many products each employed worker made) WHERE company = 2;
RESULT : 结果:
John:2
Mike:1
Jim:1
EDIT : What i tried so far: 编辑:到目前为止我尝试过的是:
I tried grouping by worker. 我尝试过按工人分组。
SELECT product FROM products WHERE company=2 GROUP BY worker;
but i dont know how to count them afterwards, meaning number of products for every worker since what i need are numbers, not actual sorted list. 但我不知道以后如何计算它们,这意味着每个工人的产品数量,因为我需要的是数字,而不是实际的排序列表。
I also tried using COUNT(*)
instead of product
but didnt know how to connect worker names with their count. 我还尝试使用COUNT(*)
代替product
但不知道如何将工人名称与其计数连接起来。
something like this, 像这样
SELECT worker, COUNT(*) totalProducts
FROM tableName
WHERE company = 2
GROUP BY worker
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.