简体   繁体   English

在“公司简介”上按热门十大公司的SQL-还是更好的解决方案?

[英]SQL for Top10 Companies by Hits on Company Profile - or a better solution?

I browsed summaries for all 500+ answers related to this question but found no apparent SQL solution to my problem. 我浏览了与该问题相关的所有500多个答案的摘要,但是没有发现针对我的问题的明显SQL解决方案。 Maybe there is none. 也许没有。

I wish to display a Top10 Companies by Hits on Company Profile from nnn to nnn time period. 我希望在nnn到nnn的时间段内,在“公司简介”上显示“按点击次数排名前10位的公司”。

MySQL Table to track hits on company profile MySQL Table追踪公司资料的点击

CREATE TABLE `hit_company` (
  `id` mediumint(9) unsigned NOT NULL AUTO_INCREMENT,
  `hitdate` date NOT NULL DEFAULT '0000-00-00',
  `customerid` mediumint(9) unsigned NOT NULL DEFAULT '0',
  `organization` varchar(80) DEFAULT NULL,
  `hitstamp` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `remote_addr` varchar(20) DEFAULT NULL,
  `hittime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
   PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=123456 ;

The MySQL query for a 24 hour report looks like this: 24小时报表的MySQL查询如下所示:

SELECT hitstamp,hitdate,customerid,organization, COUNT(organization) AS 'count' FROM hit_company 
WHERE hitstamp >= '2014-01-12 21:23' AND hitstamp <= '2014-01-13 21:23' 
GROUP BY customerid ORDER BY count DESC LIMIT 10

This yields a Top10. 这产生了Top10。 The problem is that I am getting hits significantly inflated by varying remote_addr who refresh their company profile page and thus artificially increase their hit counter. 问题是,通过更改remote_addr刷新我的公司资料页并人为地增加了点击数,我的点击量大大增加了。 I only want to count 1 vote per IP per company within the report time range. 我只想在报告时间范围内为每个公司的每个IP分配1票。

I have considered but am not sure how to code something similar to: 我也考虑过,但我不知道如何类似的代码:

WHERE COUNT(remote_addr < 2)
or
GROUP BY customerid, remote_addr HAVING 'votes' < 2

... so that the same IP address is not counted more than once per company in the final result. ...这样,在最终结果中,每个公司对同一IP地址的计数不会超过一次。

What is the SQL to do this? 什么是SQL执行此操作?

If there is no sql, what is the optimum solution in php/mysql ? 如果没有sql,php / mysql中的最佳解决方案是什么?

Thank you. 谢谢。

Note While this report spans 24 hours, other reports span from 12 hours to 30 days to all time. 注意虽然此报告跨越24小时,但其他报告则跨越12个小时至30天不等。

SELECT whatever FROM (
    SELECT whatever FROM hit_company
    WHERE hitstamp >= whatever
    GROUP BY organization, remoteaddr
) GROUP BY customerid ORDER BY count

Hand written not tested. 手写未测试。 The basic idea is use subquery. 基本思想是使用子查询。 It'll be very slow if computing on large data. 如果对大数据进行计算,将会非常慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM