【问题标题】:Find max distinct in Mysql在Mysql中查找最大不同
【发布时间】:2013-05-20 08:15:19
【问题描述】:

我有一个记录uniqueID, location (country code), IPaddressdate of visitors 的数据库。我需要制作一个 sql 语句,为每个唯一日期的每个 IP 地址构建一个 uniqueID 访问的最大次数列表(和计数)。你们中的任何人都可以提供一些吗样本、提示?

谢谢,加里

【问题讨论】:

  • 一般来说,你想要 MySQL Aggregate Functions。我无法从问题中判断您是否需要 MAXCOUNT 或两者兼而有之。如果您发布表结构、一些示例记录和预期结果,您可能会很快得到答案。
  • 没有架构很难给出示例。

标签: mysql select unique distinct


【解决方案1】:

我认为你只需要一个简单的聚合:

select IpAddress, date, count(*)
from t
group by IpAddress, date;

如果您想统计不同的访问者并将他们放在一个列表中,您需要一个访问者 ID。也许这就是uniqueId 的意义所在。如果是这样:

select IpAddress, date, count(distinct UniqueId),
       group_concat(distinct UniqueId)
from t
group by IpAddress, date;

【讨论】:

  • 这不是我们想要的简单聚合 .. 这是我们的测试脚本 ... SELECT uniqueID, MAX(Date) AS LatestAccess, COUNT(DISTINCT Location) AS DifferentCountries, COUNT(DISTINCT IPAddress) AS DistinctIPCount, COUNT(DISTINCT Date, IPAddress) AS DistinctDate FROM History WHERE (uniqueID30002) GROUP BY uniqueID ORDER BY DistinctDate DESC
【解决方案2】:

Select uniqueId , ipAddress , uniqueDate , count (*) as visitsTotal From utable Group by uniqueId,ipAddress , uniqueDate

Group By 子句确保唯一性

【讨论】:

    【解决方案3】:

    试试这个:

    select ip, dateVisited, count(*) group by ip, dateVisited
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-26
      • 1970-01-01
      • 2014-12-27
      相关资源
      最近更新 更多