1

我有一个名为 NetLogs 的 MySQL 表

servername   , session , label     , occurence
| Nost       |   11973 | Unknown   |   1 |
| Nost       |   11976 | Scan      |  74 |
| Nost       |   11976 | Unknown   |  35 |
| Gold       |   11998 | Attack    |   1 |

我需要得到

Nost | 11973|unknown|1|
Nost| 11976 |Scan | 74|
Gold|11998|Attack|1|

因此。

我试过了:

select t1.* from NetLogs t1 left join NetLogs t2 
on t1.servername=t2.servername and t1.session=t2.session and t1.occurence < t2.occurence 
where t2.occurence is null;

但我收到错误 1137 - 无法重新打开表

我也会满足于相同的结果,而不是最后出现的次数。

所以下面的一些解决方案对我不起作用,所以我将表重新制作为常规表而不是临时表并且它们有效,这让我认为我可能有一个工作查询,在某一时刻,但得到了错误,因为我在临时表上运行它...

这正是我想要的效果:

select a.* from NetLogs a where a.occurence = ( Select max(occurence)occurence from NetLogs b where a.session = b.session and a.serverName = b.serverName);

4

2 回答 2

2
SELECT  a.*
FROM    NetLogs a
        INNER JOIN
        (
            SELECT  session, MAX(occurence) occurence
            FROM    NetLogs
            GROUP   BY session
        ) b ON a.session = b.session AND
                a.occurence = b.occurence

另一种方式,

SELECT  a.*
FROM    NetLogs a
WHERE   a.occurence = 
        (
            SELECT  MAX(occurence) occurence
            FROM    NetLogs b
            WHERE   a.session = b.session
        ) 
于 2013-09-04T08:36:12.530 回答
0

询问:

SQLFIDDLE示例

SELECT  a.servername,
        a.session,
        a.label,
        a.occurence
FROM    NetLogs a
 LEFT JOIN NetLogs b
  ON b.session = a.session
  AND a.occurence < b.occurence
WHERE b.session IS NULL

结果:

| SERVERNAME | SESSION |   LABEL | OCCURENCE |
|------------|---------|---------|-----------|
|       Nost |   11973 | Unknown |         1 |
|       Nost |   11976 |    Scan |        74 |
|       Gold |   11998 |  Attack |         1 |
于 2013-09-04T08:50:06.057 回答