I'm trying to count multiple entries in a MySQL database, I know how to use COUNT(), but the exact syntax I want to get the results I need eludes me.
我正在尝试计算MySQL数据库中的多个条目,我知道如何使用COUNT(),但我想要得到的结果的确切语法是我需要的。
The problem: Table structure: ID, CODE, AUTHOR, COUNTRY, TIMESTAMP.
问题:表结构:ID,CODE,AUTHOR,COUNTRY,TIMESTAMP。
Code, Author and Country overlap many times in the table. I am trying to discover if there is one simple query that can be ran to return (using WHERE clause on COUNTRY) the author field, the code field, and then a final field that counts the number of times the CODE was present in the query result.
代码,作者和国家/地区在表格中多次重叠。我试图发现是否有一个简单的查询可以运行返回(使用COUNTRY上的WHERE子句)作者字段,代码字段,然后是一个最终字段,计算查询中CODE的出现次数结果。
So, theoretically I could end up with an array like:
所以,理论上我最终可能得到一个数组:
array('author', 'code', 'codeAppearsNTimes');
Authors also have varying codes associated with them, so I don't want the results merged. I suppose the end result would be: 'This author is associated with this code this many times'.
作者也有不同的代码,因此我不希望结果合并。我想最终的结果是:'这位作者多次与此代码相关联'。
Is this possible with MySQL?
这可能与MySQL有关吗?
Thanks in advance.
提前致谢。
1 个解决方案
#1
SELECT author, code, COUNT(*)
FROM table
WHERE country = @country
GROUP BY
author, code
#1
SELECT author, code, COUNT(*)
FROM table
WHERE country = @country
GROUP BY
author, code