如何在mysql中按状态(某人进入然后退出)对行进行配对

如何在mysql中按状态(某人进入然后退出)对行进行配对,mysql,rows,procedure,Mysql,Rows,Procedure,我有一个表user_status,在其中插入行:who、status(已输入/已退出)、what time。表如下所示: id user_id status status_date 94 5 Entered 2015-03-30 10:43:44 95 5 Exited 2015-03-30 10:47:38 96 5 Entered 2015-03-30 10:49:12 97 3 Entered 2015-03

我有一个表user_status,在其中插入行:who、status(已输入/已退出)、what time。表如下所示:

id  user_id  status   status_date
94  5        Entered  2015-03-30 10:43:44
95  5        Exited   2015-03-30 10:47:38
96  5        Entered  2015-03-30 10:49:12
97  3        Entered  2015-03-30 10:51:14
98  3        Exited   2015-03-30 11:04:12
99  5        Exited   2015-03-30 11:16:50
100 3        Entered  2015-03-30 11:20:48
101 5        Entered  2015-03-30 11:21:37
102 2        Exited   2015-03-30 11:24:47
103 2        Entered  2015-03-30 11:25:01
id  user_id  status_date_start    status_date_end
1  5         2015-03-30 10:43:44  2015-03-30 10:47:38
2  5         2015-03-30 10:49:12  2015-03-30 11:16:50
3  3         2015-03-30 10:51:14  2015-03-30 11:04:12
...
现在我想创建一个过程,将特定用户的行与他/她输入的状态匹配,并返回临时表。结果应该是这样的:

id  user_id  status   status_date
94  5        Entered  2015-03-30 10:43:44
95  5        Exited   2015-03-30 10:47:38
96  5        Entered  2015-03-30 10:49:12
97  3        Entered  2015-03-30 10:51:14
98  3        Exited   2015-03-30 11:04:12
99  5        Exited   2015-03-30 11:16:50
100 3        Entered  2015-03-30 11:20:48
101 5        Entered  2015-03-30 11:21:37
102 2        Exited   2015-03-30 11:24:47
103 2        Entered  2015-03-30 11:25:01
id  user_id  status_date_start    status_date_end
1  5         2015-03-30 10:43:44  2015-03-30 10:47:38
2  5         2015-03-30 10:49:12  2015-03-30 11:16:50
3  3         2015-03-30 10:51:14  2015-03-30 11:04:12
...

我尝试了双内部连接,用户状态上的游标,但我没有成功。请帮助

只需稍微处理一下,并在查询中使用一个额外的select

-- set up some test data
GO 
DECLARE @moves TABLE
    (
        id INT,
        user_id INT,
        status NVARCHAR(MAX),
        status_date DATETIME
    )

INSERT INTO @moves VALUES (94, 5 , 'Entered', '2015-03-30 10:43:44')
INSERT INTO @moves VALUES (95, 5 , 'Exited', ' 2015-03-30 10:47:38')
INSERT INTO @moves VALUES (96, 5 , 'Entered', '2015-03-30 10:49:12')
INSERT INTO @moves VALUES (97, 3 , 'Entered', '2015-03-30 10:51:14')
INSERT INTO @moves VALUES (98, 3 , 'Exited', '2015-03-30 11:04:12')
INSERT INTO @moves VALUES (99, 5 , 'Exited', '2015-03-30 11:16:50')

-- original data --
SELECT * FROM @moves


-- selecting the exit date into the original data --
SELECT 
    m.id
    ,m.user_id
    ,m.status_date
    ,(
        SELECT TOP 1  status_date
        FROM @moves x 
        WHERE x.user_id = m.user_id
        AND x.status = 'Exited'
        AND x.id > m.id
    ) as 'exit'
FROM @moves m  
WHERE m.status ='Entered'
结果:


我正在做一个项目,就像您现在做的一样,我将它们存储为完整的原始表。对于原始表,我存储一个\ I、用户\ id、状态\日期。我的网站代码将决定它是进入还是退出。如果输入,我将
插入完整值(“”,user\u id,last\u id“”)
哪个last\u id是从原始表返回的A\u id。如果它正在退出,我将
更新全套状态\u date\u end=last\u id,其中id=this\u id
。我使用mysql,所以唯一的更改是将TOP 1更改为LIMIT 1,它可以工作!谢谢@JensB,你太棒了