我有一个包含以下列的网站访问表:
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中进一步处理结果?
答案 0 :(得分:1)
试试这个::
Select *, MAX(timestamp_col) as lastVisit from site_table
GROUP BY HOSTS order by lastVisit desc
答案 1 :(得分:1)
您不能ORDER BY
后跟LIMIT
,然后是ORDER BY
,因此您必须使用子查询:
SELECT * FROM (SELECT *
FROM visits
ORDER BY timestamp DESC
LIMIT 200
) visits2
ORDER BY host
答案 2 :(得分:0)
如果你想要最后的200
,你必须限制结果 SORT BY timestamp DESC, host LIMIT 0, 200
如果您再次出现不同的访问者,请使用
SELECT DISTINCT visitor FROM ....
答案 3 :(得分: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
)
)
我不测试这个疯狂的代码...但我希望它能给你一些方法:)
答案 4 :(得分: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;