如何加入相同的 table 以检查 a>b 是否为 10

How to join same table to check if a>b with 10

Table mytable

id      numbers       whereonly

1       2                1
2       35               1
3       22               1
4       20               1
5       3                1
6       70               1
7       80.15925         1
8       60               7
9       50               7

我需要按 numbers 排序并使用 id 1 进行搜索,直到我找到一个具有 numbers 行且 10 更大的 id .
期望的结果:2、20、35、70、80.15925
仅当列 whereonly 为 1 时
有办法吗?

你可以试一试:

SELECT DISTINCT t1.id AS id, t1.numbers AS numbers
FROM table AS t1
INNER JOIN table AS t2 ON t1.numbers > t2.numbers - 10
WHERE t1.whereonly = 1
GROUP BY t2.numbers
ORDER BY t1.numbers;

这里是 sqlfiddle.

Edit 1: As strawberry suggested

SELECT DISTINCT x.* 
  FROM mytable x
  JOIN
     ( SELECT t2.numbers t2n
            , MIN(t1.id) id
         FROM mytable t1
         JOIN mytable t2 
           ON t1.numbers > t2.numbers - 10
        GROUP 
           BY t2.numbers
     ) y
    ON y.id = x.id
    ORDER BY x.numbers
    WHERE x.whereonly = 1;

这里是 sqlfiddle.