大佬教程收集整理的这篇文章主要介绍了php – 根据mySQL中的COUNT()值限制GROUP BY,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在将事件记录到MysqL数据库中,并希望获得前3个事件用于监视目的.@H_801_2@
我的表事件日志如下所示:@H_801_2@
+----+------------------+---------------------+
| id | eventname | eventdate |
+----+------------------+---------------------+
| 0 | machine1.started | 2016-09-04 19:22:23 |
| 1 | machine2.reboot | 2016-09-04 20:23:11 |
| 2 | machine1.stopped | 2016-09-04 20:24:12 |
| 3 | machine1.started | 2016-09-04 20:25:12 |
| 4 | machine1.stopped | 2016-09-04 23:23:16 |
| 5 | machine0.started | 2016-09-04 23:24:00 |
| 6 | machine1.started | 2016-09-04 23:24:16 |
| 7 | machine3.started | 2016-09-04 23:25:00 |
| 8 | machine4.started | 2016-09-04 23:26:00 |
| 9 | cluster.alive | 2016-09-04 23:30:00 |
| 10 | cluster.alive | 2016-09-05 11:30:00 |
+----+------------------+---------------------+
查询最终应返回以下内容,持有@H_801_2@
>最常发生的前3个事件(基于MysqL的COUNT()函数生成的列事件计数),按事件名称分组
>只有2行,其中eventcount = 1,但仅当1位于前3个事件计数内时(因为有很多事件发生在
一次,因此会超载我的前端)@H_801_2@
+------------+------------------+
| eventcount | eventname |
+------------+------------------+
| 3 | machine1.started |
| 2 | machine1.stopped |
| 2 | cluster.alive |
| 1 | machine0.started |
| 1 | machine2.started |
+------------+------------------+
请注意,我不需要仅返回3行,而是具有3个最高事件数量的行.@H_801_2@
我通过搞乱下面的查询字符串进行了大量的实验,包括多个选择和可疑的CASE … WHEN条件,但是无法使其按照我需要的方式工作.@H_801_2@
@R_944_10288@CT COUNT(id) AS 'eventcount',eventname
FROM eventlog
GROUP BY eventname
ORDER BY eventcount DESc;
以高效的方式获得理想结果的最佳方法是什么?
@R_944_10288@CT
t2.eventcount,t2.eventname
FROM
(
@R_944_10288@CT
t.eventname,t.eventcount,@Rank:=IF(@PrevCount=t.eventcount,@Rank,@Rank+1) Rank,@CountRownum:=IF(@PrevCount=t.eventcount,@CountRowNum + 1,1) CountRowNum,@PrevCount:= t.eventcount
FROM
(
SELECT
l.eventname,COUNT(*) as eventcount
FROM
eventlog l
GROUP BY
l.eventname
ORDER BY
COUNT(*) DESC
) t
CROSS JOIN (SELECT @Rank:=0,@CountRowNum:=0,@PrevCount:=-1) var
ORDER BY
t.eventcount DESC
) t2
WHERE
t2.Rank < 4="" and="" not="" (t2.eventcount="1" and="" t2.countrownum="" >="" 2)="">
以上是大佬教程为你收集整理的php – 根据mySQL中的COUNT()值限制GROUP BY全部内容,希望文章能够帮你解决php – 根据mySQL中的COUNT()值限制GROUP BY所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。