MySQL在select中选择

我甚至不确定标题是否正确,如果不是就道歉!

我有一个MySQL数据库,其中包含从多个接入点收集的Wi-Fi设备的详细信息.

我的下面的SELECT语句返回在1小时内找到的所有设备:

SELECT mac 
  FROM TBLWiFi 
GROUP BY 
       mac 
HAVING COUNT( mac ) >1 
       AND TIMESTAMP >= DATE_SUB( NOW( ) , INTERVAL 1 HOUR )
)

结果如下所示:

╔═══════════════════╦══════════════════╦═════════════╗
║ mac               ║ timestamp        ║ reportedby  ║
╠═══════════════════╬══════════════════╬═════════════╣
║ B0:02:94:1D:3B:43 ║ 21/03/2014 12:27 ║ AP-3c9b33d0 ║
║ C0:3E:0F:0F:2D:EB ║ 21/03/2014 12:37 ║ AP-3c9b33d0 ║
║ 64:70:02:29:16:AE ║ 21/03/2014 12:32 ║ AP-3c9b33d0 ║
║ B0:02:94:1D:3B:43 ║ 21/03/2014 12:46 ║ AP-5c9b33d0 ║
║ B4:F0:AB:C2:F6:92 ║ 21/03/2014 12:46 ║ AP-3c9b33d0 ║
║ 00:26:AB:F1:7C:EE ║ 21/03/2014 12:45 ║ AP-5c9b33d0 ║
║ B0:02:94:1D:3B:43 ║ 21/03/2014 12:45 ║ AP-4c9b33d0 ║
║ 1C:C1:DE:6A:90:47 ║ 21/03/2014 12:56 ║ AP-4c9b33d0 ║
║ B4:F0:AB:C2:F6:92 ║ 21/03/2014 13:02 ║ AP-3c9b33d0 ║
║ 00:26:AB:F1:7C:EE ║ 21/03/2014 13:07 ║ AP-4c9b33d0 ║
╚═══════════════════╩══════════════════╩═════════════╝

我现在想要做的是从这个列表中找出多个设备(mac)被多个接入点报告(报告).

我处于一个平坦的旋转中,我的思绪已经完全消失了.任何帮助,将不胜感激.

最佳答案 首先,您的查询应如下所示:

SELECT mac 
FROM TBLWiFi 
WHERE TIMESTAMP >= DATE_SUB( NOW( ) , INTERVAL 1 HOUR )
GROUP BY  mac 
HAVING COUNT( mac ) > 1 ;

在having子句中,timestamp上的条件仅比较每个mac的一个时间戳.被比较的那个来自任意行.

要获得多个访问点,请将having子句更改为:

HAVING COUNT(mac) > 1 and
       COUNT(DISTINCT reportedby) > 1;

实际上,如果有多个报告,那么有多行,所以这就足够了:

HAVING COUNT(DISTINCT reportedby) > 1;
点赞