mysql显示最近的`ip`

时间:2014-07-15 14:20:08

标签: php mysql sql mysqli

 SELECT *, COUNT(`id`) as count 
 FROM `visitors` 
 WHERE `time` = max(`time`) 
 GROUP BY `ip` 
 ORDER BY `time` DESC";

这是我的查询显示来自我的访问者表格的数据,他们通过ip对它们进行分组,但我想显示该特定ip的最近时间;

对于前。如果这个ip:145.46.78.241被打开10次我想显示最新的那个,我的意思是最好的UNIX时间戳。

#   ip               location             isp                           time

5   79.171.53.96    Tirana, Tirane, AL   AS30722 Vodafone Omnitel B.V.  58 minutes ago
11  46.252.36.216   AL                   AS52073 4ALB                   2 hours ago
2   79.171.53.100   Tirana, Tirane, AL   AS30722 Vodafone Omnitel B.V.  2 hours ago

现在为前。 ip = 79.171.53.96最近比58分钟开放,我想显示那个不是58分钟前打开的那个。

我的查询无效,我收到此错误:Fatal error: Call to a member function fetch_assoc() on a non-object

1 个答案:

答案 0 :(得分:1)

SELECT ip, max(time) as 'Time', count(ip) as '#'
FROM visitors
GROUP BY ip
ORDER BY max(time) DESC

另外你可以使用

 max(location) as 'Location', max(isp) as 'ISP' 
如果需要,在SELECT语句中