简体   繁体   中英

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 can only ever be 1, 2 or 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 .

Basically, this table collects GPS data ( Latitude and Longitude ) for a journey.

EventType = 1 means Start of Journey

EventType = 2 means Everything between the Start and End of a Journey

EventType = 3 means End of Journey

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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