M
M
Max Payne2017-07-14 02:59:22
MySQL
Max Payne, 2017-07-14 02:59:22

How to organize such a double request?

I need to make a double selection - first select all the values ​​from the table where side="bright", and then find the closest values ​​to my given value in the results (for example, 100). I understand that in the second one there should be something like ABS(100 - results) and you need to use a double query, but I don’t understand how to implement it at all.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AlexKeller, 2017-07-14
@YardalGedal

Something like?

SELECT *, ABS(100 - results) AS diff
FROM t
WHERE side = 'bright'
ORDER BY diff ASC
LIMIT 5

D
Dmitry Ponomarev, 2017-07-14
@dimker

I think you need something like

SELECT * 
  FROM (
    SELECT TOP 10 ABS(100 - result) as diff
    FROM t
    WHERE side = 'bright') b
  WHERE diff < 20

Where 100 is your target value and 20 is the error, i.e. "nearest values" that differ by no more than 20

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question