如何比较表中的每一行与所有行?

时间:2013-06-12 11:22:07

标签: mysql join

我正在使用mysql。

将每一行与此表中的所有左行进行比较,并选择HEADING介于-15<之间的ID和时间戳。标题(从最后一行)< +15和-10<时间戳(从最后一行)< 10。

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

EG。第一个表行:

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

如您所见,ID不是顺序的,所以我不能这样做吗?

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)

1 个答案:

答案 0 :(得分:1)

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)