MySQL:按多列分组未给出准确的结果

标签 mysql database group-by

我有一个包含 5 列的表,即:

before_1, before_2, before_3, rule, name

其中 before_1、before_2 和 before_3 是文档中名称/单词之前的三个单词。

我想找到的是:

Which are the two words that occur together before a name. I don't want just the top words, but all the words sorted by the number of occurrences.

我尝试了以下几个查询,但这对我不起作用。

select count(before_2),count(before_3),name from data_with_before_words group by name;

两列的计数相同,这不是我所期望的。

示例数据:

前 5 行:

before_1,before_2,before_2,rule,name

a,league,of,Persona,Amell
the,assasin,of,Persona,Amell
the,league,of,Persona,Amell
a,assasin,of,Persona,Amell
a,league,of,Persona,Amell

预期输出:

league,of,3,Amell
assasin,of,2,Amell

如有任何帮助,我们将不胜感激。

最佳答案

要获得预期的输出,您可以使用以下查询

select before_2,before_3,name,count(*)
from data_with_before_words 
group by before_2,before_3,name
order by count(*) desc

Demo

关于MySQL:按多列分组未给出准确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47827261/

相关文章:

mysql - 如何更改在 MAMP 上运行的 Django 项目数据库名称?

php - 以 PHP JQUERY AJAX 形式搜索并显示

sql - 有没有办法在不执行未知数据库查询的情况下获取类型/名称?

sqlite - SQL 查询 : How to count occurrence of a specific value in a column without grouping on that column?

c# - 在特定范围内按日期分组

mysql - 计算 MYSQL 中具有特殊条件的重复条目

php - 为什么我无法访问我的管理界面?

sql-server - 使用 QSqlQuery 检索 SCOPE_IDENTITY()

objective-c - 在 iOS 应用程序上保存数据的最有效方式

mysql - 条件 GROUP BY 结果