我有一个带有一组文本的DB表,每个文本都有五(5)种情绪。我需要做的是让每行中最频繁的情绪出现,只要该情绪出现至少3次。否则,我不想在结果集中显示汇总的行。
例如,我有:
text | emotion
nice day | happyness
nice day | happyness
nice day | neutral
nice day | neutral
nice day | happyness
hello | sadness
hello | sadness
hello | surprise
hello | surprise
hello | neutral
输出应为:
美好的一天快乐
在这种情况下,由于不会出现至少3次情绪,因此不会显示文本“ Hello”。
我尝试了许多解决方案,但找不到可行的解决方案...
最佳答案
这有点棘手:
select text, emoticon
from t
group by text, emoticon
having count(*) >= 3 and
count(*) = (select count(*)
from t t2
where t2.text = t.text
group by t2.emoticon
order by count(*) desc
limit 1
);
关于mysql - MySQL:提取元素的最频繁值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48543870/