Wasif Bashir
Wasif Bashir

Reputation: 13

Getting result close to certain number

Following is my sqlfiddle in which I am trying to display result against the given name and as per its feature number (the number against the given name) my query should generate results and show all those names and feature number that are close to it. For example (from following table) the user searches for jhon and its feature number is 20 then my query should show result for numbers that are close to 20 like 18,19,21,22 I want to set the closing limit to 2. Like for number 20 the close limit is 18,19,21,22.

Kindly let me know how can I do it. Thanks,

http://www.sqlfiddle.com/#!2/c177a/1

+--------------------+
| NAME    |  Feature |
+--------------------+
| Jhon    |    20    |
| Jame    |    18    |
| Jimm    |    21    |
| Kim     |    30    |
+--------------------+ 

Upvotes: 1

Views: 52

Answers (1)

John Woo
John Woo

Reputation: 263933

SELECT  a.*
FROM    searching a
        INNER JOIN searching b
            ON a.feature BETWEEN b.feature - 2 AND b.feature + 2
WHERE   b.name = 'gore'

Upvotes: 4

Related Questions