我正在使用带有MySQL后端的freeradius服务器,我正在尝试从radacct表中选择已登录的用户。该表看起来像:
╔══════════╦═══════════════╦═════════════════════╦═════════════════════╗
║ USERNAME ║ ACCTSESSIONID ║ ACCTSTARTTIME ║ ACCTSTOPTIME ║
╠══════════╬═══════════════╬═════════════════════╬═════════════════════╣
║ test2 ║ 00000051 ║ 2013-09-06 15:44:05 ║ 2013-09-06 15:44:26 ║
║ jason ║ 00000057 ║ 2013-09-06 15:44:56 ║ 2013-09-06 15:46:08 ║
║ jason ║ 00000058 ║ 2013-09-06 15:44:56 ║ 2013-09-06 15:46:08 ║
║ test1 ║ 00000059 ║ 2013-09-06 15:44:57 ║ 2013-09-06 15:46:08 ║
║ test1 ║ 0000005A ║ 2013-09-06 15:44:57 ║ 2013-09-06 15:46:08 ║
║ test2 ║ 0000005B ║ 2013-09-06 15:44:57 ║ 2013-09-06 15:46:08 ║
║ jason ║ 00000061 ║ 2013-09-06 15:46:39 ║ NULL ║
║ jason ║ 00000062 ║ 2013-09-06 15:46:39 ║ NULL ║
║ test1 ║ 00000063 ║ 2013-09-06 15:46:39 ║ NULL ║
║ test1 ║ 00000064 ║ 2013-09-06 15:46:39 ║ NULL ║
║ test2 ║ 00000065 ║ 2013-09-06 15:46:39 ║ NULL ║
╚══════════╩═══════════════╩═════════════════════╩═════════════════════╝
每一行都是一个登录会话,'acctstoptime'为空的行被认为是活动的。我正在尝试获取每个用户名的用户名和acctsessionid,该用户名的行数超过1行,且acctstoptime为空。
答案 0 :(得分:0)
这是获得指定结果的一种方法:
SELECT s.username
, s.acctsessionid
FROM (
SELECT r.username
FROM radacct r
WHERE r.acctstoptime IS NULL
GROUP BY r.username
HAVING COUNT(1) > 1
) d
JOIN radacct s
ON s.username = d.username
WHERE s.acctstoptime IS NULL
ORDER
BY s.username
, s.acctsessionid
答案 1 :(得分:0)
试试这个:
SELECT T1.USERNAME,
T1.ACCTSESSIONID
FROM (SELECT *
FROM TABLEB
WHERE ACCTSTOPTIME IS NULL) T1
INNER JOIN (SELECT USERNAME
FROM TABLEB
WHERE ACCTSTOPTIME IS NULL
GROUP BY USERNAME
HAVING COUNT(*) > 1)T2
ON T1.USERNAME = T2.USERNAME
可以在 SQL Fiddle 找到一个工作示例。
祝你好运!