简体   繁体   中英

How to compare each row from table with all rows?

Im using mysql.

Compare each row with all left rows from this table and select the ID and timestamp where HEADING is between -15 < Heading (from last row) < +15 AND -10 < timestamp (from last row) < +10.

myTable
id
heading (In degrees)
timestamp (in seconds)

Eg. of the 1st table rows:

1 - 50 - 133324565
2 - 12 - 133324565
3 - 10 - 133324565
5 - 220 - 133324565
6 - 35 - 133324565
10 - 10 - 133324565
11 - 15 - 133324565
12 - 22 - 133324565
16 - 168 - 133324565

As you see, ID's are not sequential, so I can't do this right?

SELECT T1.id,T1.heading,T2.id,T2.heading
FROM mytable as T1, mytable as T2
WHERE T1.id > T2.id -1
AND (T1.timestamp > T2.timestamp -10 OR T1.timestamp < T2.timestamp +10)
AND (T1.heading > T2.heading -15 OR T1.heading < T2.heading +15)
SELECT T1.id,T1.heading,T2.id,T2.heading
FROM mytable as T1, mytable as T2
WHERE T1.id > T2.id AND NOT EXISTS (SELECT T3.id FROM mytable as T3 WHERE T3.id<T1.id AND T3.id>T2.id)
AND (T1.timestamp > T2.timestamp -10 OR T1.timestamp < T2.timestamp +10)
AND (T1.heading > T2.heading -15 OR T1.heading < T2.heading +15)

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