获取所有行,直到匹配一个特殊行

时间:2013-08-02 09:25:22

标签: mysql sql fetch

我有一个登录MySql数据库,现在我会用它来检查会话,所以我想在“类型3”之前获取所有行而不是更多。

+--------+-------------+---------------------+-----------+---------+------+
| id     | username    | date                | address   | success | type |
+--------+-------------+---------------------+-----------+---------+------+
| 110771 | test        | 2013-08-02 11:17:54 | 127.0.0.1 |       1 |    5 |
| 110772 | test        | 2013-08-02 11:18:03 | 127.0.0.1 |       1 |    3 |
| 110773 | test        | 2013-08-02 11:18:13 | 127.0.0.1 |       1 |    7 |
| 110774 | test        | 2013-08-02 11:18:17 | 127.0.0.1 |       1 |    5 |
+--------+-------------+---------------------+-----------+---------+------+

我该怎么做?

1 个答案:

答案 0 :(得分:2)

这是一种方式:

SELECT *
FROM logTable a
WHERE NOT EXISTS
(
    SELECT 1
    FROM logTable b
    WHERE b.date <= a.date
    AND b.type = 3
)