SQL - find the row with the values โ€‹โ€‹of the two columns closest to X and Y

I am creating a website where people can get information based on their weight and height. How to structure a query that will give me a string with two specific values โ€‹โ€‹closest to those that users enter?

I found this one on stackoverflow and it was very useful. I am trying to do something similar to this with only 2 values โ€‹โ€‹set to 1.

+5
source share
3 answers

If weight and height are of equal importance, you can use this (linear)

select top 1 *
from tbl
order by ABS(weight-@weight) + ABS(height-@height)

, , 0,01 (1 ) equal 1 . , 5 5 "", 10 0 .

(, )

select top 1 *
from tbl
order by ABS(weight-@weight)^2 + (ABS(height-@height)*100)^2
+3

, SQL, , ( , ..), , .

+1

? , 6 " , 6 . :

SELECT * FROM table
ORDER BY ABS(weight - @weight) + ABS(height - @height)*3

Itโ€™s just me taking a swing, trying to make the difference in height more important. In this example, I give it a weight of 3. This example assumes the height of the person is in inches ... If you have this in centimeters or something, this might work better.

0
source

All Articles