Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL查询以查找仍在房间内的用户_Mysql_Sql_Database - Fatal编程技术网

MySQL查询以查找仍在房间内的用户

MySQL查询以查找仍在房间内的用户,mysql,sql,database,Mysql,Sql,Database,下面是我的数据库表,在这里我将有参加会议室的登记和退房记录 id registration_id roomno day type 1 101 1 2 In 2 103 1 2 In 3 101 1 2 Out 4 105 1 2 In 5 103 1 2 Out 6 101

下面是我的数据库表,在这里我将有参加会议室的登记和退房记录

id  registration_id roomno day type
1   101             1      2   In
2   103             1      2   In
3   101             1      2   Out
4   105             1      2   In
5   103             1      2   Out
6   101             1      2   In
7   103             1      2   In
8   101             1      2   Out
9   105             1      2   In
10  103             1      2   Out
现在,我想选择那些仍在出席会议的记录。条件类似于他们的最后一条记录应该是
type=In
。一天中每个用户可以有多个输入/输出条目

请让我知道最快的MySQL查询

谢谢

我最后使用的答案:

select * from `registrations_inouts` t 
group by t.registration_id 
having max(id) = max(case when type = 'In' then id end) 
order by rand() limit 1; 

这里有一个使用
的方法不存在

select *
from t
where t.type = 'In' and
      not exists (select 1
                  from t t2
                  where t2.registration_id = t.registration_id and t2.type = 'Out'
                 );
另一种方法使用条件聚合:

select t.registration_id
from t
group by t.registration_id
having max(id) = max(case when type = 'In' then id end);

注意:这两种方法都假定ID是按时间顺序分配的,因此较大的ID在时间上会较晚。

这里有一种使用
的方法不存在:

select *
from t
where t.type = 'In' and
      not exists (select 1
                  from t t2
                  where t2.registration_id = t.registration_id and t2.type = 'Out'
                 );
另一种方法使用条件聚合:

select t.registration_id
from t
group by t.registration_id
having max(id) = max(case when type = 'In' then id end);

注意:这两种方法都假设ID是按时间顺序分配的,因此较大的ID在时间上更晚。

这就像一个符咒,感谢您的快速响应:)这就像一个符咒,感谢您的快速响应:)