2

我有一个包含以下列的站点访问表:

timestamp
host
url

我想查看最后几次(例如 200 次)访问,但同时查看同一主机的组访问(查看他/她访问的页面顺序)。

如果我做:

... SORT BY timestamp DESC, host

如果多个访问者同时在线,我会混淆主机

如果我做:

... SORT BY host, timestamp DESC

我没有得到最新的访问(只有每个主机的最新访问)

我想看看例如

9:01 | a.com | /index
9:05 | a.com | /interesting
9:07 | a.com | /contact
9:02 | b.com | /index
9:03 | b.com | /product-a
9:08 | b.com | /thanks-for-buying

这在 Mysql 中是可能的还是我应该在 php 中进一步处理结果?

4

5 回答 5

1

尝试这个::

Select *, MAX(timestamp_col) as lastVisit from site_table

GROUP BY HOSTS order by lastVisit desc
于 2012-11-15T09:25:31.503 回答
1

你不能做ORDER BYfollow byLIMIT然后 another ORDER BY,所以你必须使用一个子查询:

SELECT * FROM (SELECT * 
    FROM visits
    ORDER BY timestamp DESC
    LIMIT 200
) visits2
ORDER BY host
于 2012-11-15T09:31:08.320 回答
0

上面的答案为我指明了正确的方向,谢谢。这个有效。LIMIT 1000 是将查询时间从近 3 分钟减少到 7 秒(40000 条记录)。

SELECT v1.stamp, v1.host FROM visits AS v1 
   LEFT JOIN (SELECT MIN(date) AS firstVisit, host
              FROM visitor AS v2 
              GROUP BY v2.host
              ORDER BY firstVisit DESC
              LIMIT 1000) AS j
       ON v1.host=j.host 
       ORDER BY firstVisit DESC, v1.host, v1.date DESC
       LIMIT 100;
于 2012-11-15T22:30:54.147 回答
0

如果你想要最后 200 个,你必须限制结果

  SORT BY timestamp DESC, host LIMIT 0, 200 

如果您没有再次出现同一个访客,请使用

SELECT DISTINCT visitor FROM ....
于 2012-11-15T09:26:39.667 回答
0

我认为不可能在一个查询中使正常。你可以试试这个:

SELECT * 
FROM table AS t1 
WHERE t1.timestamp_col >= (SELECT MIN(tstamp) 
    FROM (SELECT t2.timestamp_col 
        FROM tables AS t2 
        WHERE t2.host = t1.host 
        ORDER BY timestamp_col DESC 
        LIMIT 200
    )
)

我不测试这个疯狂的代码......但我希望它能给你方法:)

于 2012-11-15T09:31:24.320 回答