听起来可能很混乱,我会尽量让它更容易理解。
有一张名为“会话”的表。
列是: rowid,时间,用户,操作。
我想要做的是选择操作为1 的最后一个(最高rowid)x(让我们说4,它不是静态的)会话,但只有当一行没有存在时间列,操作为0 ,相同用户。
因此,作为示例,只应选择粗体行:
rowid(primary) time(epoch int) user action
**152 1393635884 42 1**
152 1392799204 75 1
152 1392799416 42 0 *<-- the bolded row is selected because this exists with a greater time*
152 1392802679 16 1
如果这有帮助,伪MySQL如何工作
SELECT *
FROM sessions
WHERE action = 1 AND there is a row where time > this time and user = this user and action = 0
SORT BY rowid DESC
LIMIT 4
答案 0 :(得分:0)
我相信你可以使用not exists
条款做你想做的事情:
SELECT s.*
FROM sessions s
WHERE s.action = 1 AND
not exists (select 1
from sessions s2
where s2.user = s.user and
s2.time > s.time and
s2.action = 0
)
ORDER BY rowid DESC
LIMIT 4;