gpt4 book ai didi

MySQL:提取元素最常见的值

转载 作者:行者123 更新时间:2023-11-29 06:48:13 24 4
gpt4 key购买 nike

我有一个包含一组文本的数据库表,每个文本都有五 (5) 种情绪。我需要做的是获取每组行中最常见的情绪,前提是该情绪至少出现三次。否则,我不想在结果集中显示聚合行。例如我有:

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

输出应该是:美好的一天|幸福
在这种情况下,不会显示文本“Hello”,因为没有情绪出现至少 3 次。

我尝试了很多解决方案,但找不到有效的解决方案...

最佳答案

这有点棘手:

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:提取元素最常见的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48543870/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com