简体   繁体   English

如何选择具有特定字段值的两个记录之间的所有行?

[英]How i can Select all rows between two records with specific field values?

I have a Journeys table. 我有一个旅程表。 One of the columns on this table is EventType that holds an integer. 该表上的列之一是EventType ,它包含一个整数。

EventType can only ever be 1, 2 or 3. EventType只能是1、2或3。

How can I make a selection that has the logic: 如何选择具有逻辑的选择:

Find a record with EventType = 3 and then select all records before it until you get to a record with EventType = 1 . 查找EventType = 3的记录,然后选择之前的所有记录直到获得EventType = 1的记录。

Basically, this table collects GPS data ( Latitude and Longitude ) for a journey. 基本上,此表收集旅程的GPS数据( 纬度和经度 )。

EventType = 1 means Start of Journey EventType = 1表示旅程开始

EventType = 2 means Everything between the Start and End of a Journey EventType = 2表示旅程开始和结束之间的所有事件

EventType = 3 means End of Journey EventType = 3表示旅程结束

The table Schema is: 表模式为:

表架构

MySQL isn't very good (I can do simple Select statements ...etc) but just couldn't think of a way for attempting this. MySQL并不是很好(我可以执行简单的Select语句... etc),但是想不到尝试这种方法的方法。

I would think you could do something like this: 我认为您可以执行以下操作:

SELECT *
FROM your_table
WHERE id > (SELECT MIN(id) 
            FROM your_table
            WHERE EventType = 1)
  AND id < (SELECT MAX(id) 
            FROM your_table
            WHERE EventType =3)

This may not be the most elegant solution, but from what I tried, it worked. 这可能不是最优雅的解决方案,但是从我的尝试来看,它确实有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM