1

假设我有这张桌子:

+----+------+---------------------+-------------------------+
| id | user | timestamp           | referrer                |
+----+------+---------------------+-------------------------+
| 1  | foo  | 2012-08-26 14:05:55 | http://fubar.com/online |
+----+------+---------------------+-------------------------+
| 2  | foo  | 2012-08-26 14:05:59 | http://fubar.com/chat   |
+----+------+---------------------+-------------------------+
| 3  | bar  | 2012-08-27 07:06:07 | http://fubar.com/chat   |
+----+------+---------------------+-------------------------+
| 4  | max  | 2012-08-28 14:08:12 | http://fubar.com/chat   |
+----+------+---------------------+-------------------------+
| 5  | max  | 2012-08-28 14:08:36 | http://fubar.com/online |
+----+------+---------------------+-------------------------+

我想选择所有同时拥有超过 1 个推荐人和时间戳相同的用户,精确到分钟

这意味着在 1 分钟内,他们正在查看同一站点的 2 个页面,因此可能暗示他们是多选项卡。所以在上表中, foo 和 max 符合标准。

我想知道如何在 mysql 中实现这一点。

谢谢你。

4

2 回答 2

1

进行自加入并TIMESTAMPDIFF()在加入规则中使用:

SELECT DISTINCT a.user
FROM   my_table a JOIN my_table b ON
       a.user     =  b.user
   AND a.id       <  b.id
   AND a.referrer <> b.referrer
   AND ABS(TIMESTAMPDIFF(SECOND, a.timestamp, b.timestamp)) <= 60

sqlfiddle上查看。

于 2012-08-29T03:27:37.040 回答
0

带有“有”子句的东西怎么样?

SELECT user, COUNT(user) AS tabCount FROM whatever
 GROUP BY user
HAVING tabCount > 1;
于 2012-08-29T03:19:46.840 回答