0

我正在使用带有 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 为空。

4

2 回答 2

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
于 2013-09-09T19:34:40.037 回答
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

祝你好运!

于 2013-09-09T19:36:27.670 回答