我有一个 MySQL 数据库,看起来很像这样:
Term-----------LOB---------Date------ dde JM Part A 2016-07-01 drug waste JM Part A 2016-07-01 drug waste JM Part A 2016-07-01 Duplicates JM Part A 2016-07-01 credit balance JM Part B 2016-07-01 drug waste JM Part B 2016-07-01
所以,我想要每个 LOB(lob 是一个类别)每月的前 4(或 n)个“条款”
所以在这个例子中它可以是
JM Part A July 2016 --dde - 7 --drug waste - 5 --credit balance - 3 --duplicates - 1
JM Part B - July 2016 --dde - 7 --discharge stats- 5 --credit balances- 3 --overpayment - 1
唉,有点失落了。到目前为止我能做的最好的是
SELECT term, lob, COUNT(term) uid_count FROM qs GROUP BY lob ORDER BY uid_count DESC LIMIT 5
这似乎显示了所有 lob 和所有日期范围的前 5 个搜索词。
因此,我们将不胜感激任何帮助。我正在查看在 PHP 页面上显示报告。
感谢您的宝贵时间。
最佳答案
如果不同 Lob 的数量很少..你可以使用 union
(select Term, count(*)
from my_tbale
where Lob = 'JM Part A'
group by Term, `Date`
order by count(*) desc limit 5)
union
(select Term, count(*)
from my_tbale
where Lob = 'JM Part B'
group by Term, `Date`
order by count(*) desc limit 5)
关于php - 如何显示 MySQL 数据库中的热门搜索词并按日期分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38152983/