0

我想使用 MySQL 查询查找多帐户, SELECT DISTINCT(ip) AS ip, name FROM {table} ORDER BY timestamp ASC; 并以时尚的方式列出所有帐户:

ip, name, timestamp

1, me, 2013-09-19 19:58:52
2, me, 2013-09-19 18:00:00
123, us, 2013-09-19 20:58:52
2, you, 2013-09-19 09:58:52

但是如何用逻辑进行选择:

  1. 找到我所有名称 =“我”的 IP
  2. 获取 IP 匹配点 1 的所有行。
  3. 从第 2 点全选。但对于 1 个 ip-nick 对只有 1 行,它的时间戳是最新的

伪查询: "SELECT * FROM table WHERE name = "me" + ALL OTHER ROWS THAT MATCH "me" ip WHERE this row with IP must me the lastest timestamp;"

返回:

1, me, 2013-09-19 19:58:52
2, you, 2013-09-19 09:58:52

如果你知道前任答案的一半。如何在没有最新时间戳的情况下做到这一点我也欢迎您回答。

4

2 回答 2

2

这是#2:

SELECT t1.*
FROM Table t1
JOIN (SELECT DISTINCT ip
      FROM Table
      WHERE name = "me") me_ip
ON t1.ip = me_ip.ip

这是#3:

SELECT t2.*
FROM Table t2
JOIN (SELECT t1.ip, t1.name, MAX(t1.timestamp) maxtime
      FROM Table t1
      JOIN (SELECT DISTINCT ip
            FROM Table
            WHERE name = "me") me_ip
      ON t1.ip = me_ip.ip
      GROUP BY t1.ip, t1.name) t3
ON t2.ip = t3.ip AND t2.name = t3.name AND t2.timestamp = t3.maxtime
于 2013-09-20T18:17:10.090 回答
1

这将显示所有匹配项,而不仅仅是最新的。所以这不满足第 3 点。除非有人打败我,否则我会编辑。

SELECT  t1.*

FROM    table as t1

        JOIN (SELECT ip FROM table GROUP BY ip HAVING COUNT(*) > 1) as dup
        ON t1.ip = dup.ip

ORDER BY t1.ip
于 2013-09-20T18:11:28.360 回答