我有2张桌子:设备表和用户表。
设备表有2列:ID和MacAddress。
用户表有4列:ID,名称,电话,MacAddress。
例如,将填充用户表的固定列表:
1,Steve Marks,219-373-1485,5A:2B:3C:8D
2,Dan Marks,310-248-1455,5C:3A:2B:8A
每隔5分钟,设备表将在当地附近填充MacAddresses和设备信息。
我想创建一个视图,以获取在设备表中重复两次以上的MacAddresses的名称,电话以及用户表中是否存在相应的macaddress匹配。
谢谢!
萨姆
答案 0 :(得分:0)
您可以使用此查询
SELECT name,phone, MacAddress, COUNT(name) as total
FROM user
GROUP BY name
HAVING ( COUNT(name) > 1 )
答案 1 :(得分:0)
您可以使用此查询查找重复两次以上的名称和手机匹配的macaddresses到Device表及其相应的mac地址中找到用户表。
select D1.Name, D1.Phone D1.MacAddress from User U
join (
select MacAddress from Device
group by MacAddress
having count(MacAddress) > 2 ) D on U.MacAddress = D.MacAddress
join Device D1 on D1.MacAddress = D.MacAddress
如果您需要查找超过1次,只需更改
count(MacAddress) >= 2
答案 2 :(得分:0)
据我所知,您的Users表包含WLAN热点的所有用户。设备列表包含在给定时间访问过热点的所有mac地址。
怎么样?
SELECT name, macAddress, count(macAddress)
FROM Device
INNER JOIN USER ON User.MacAddress = Device.MacAddress
GROUP BY name, macAddress
HAVING COUNT(macAddress) >= 2
如果我理解你的用例,我建议在设备表中添加另一个时间戳列。因此,您已准备好更改数据,并能够在所需的时间段内进行选择。例如。 “谁在10点到11点之间至少有2次访问?”:
SELECT name
FROM Device
INNER JOIN USER ON User.MacAddress = Device.MacAddress
WHERE timestamp between '2016-02-14 10:00:00' and '2016-02-14 11:00:00'
GROUP by name, macAddress
HAVING COUNT(macAddress) >= 2
ORDER BY name