G
G
Gleb Nikolaev2017-05-23 12:27:21
MySQL
Gleb Nikolaev, 2017-05-23 12:27:21

How to make a BETWEEN check between a larger and smaller number?

Good afternoon!
Temporary creative block.
How to make BETWEEN check between a larger number and a smaller one.
For example, between 20 and 30 everything is ok - it works.
And between 30 and 20 is an empty result.
I do not want to make a condition which variable is larger and change places in the query. Any ideas?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
V Sh., 2017-05-23
@glebn

Add through OR the second line BETWEEN with the changed arguments. You can use the CASE structure in the query.

B
Boris Korobkov, 2017-05-23
@BorisKorobkov

I don’t want to make a condition which variable is larger and change places in the query

This is what will be the most effective solution.

V
vyrkmod, 2017-05-23
@vyrkmod

BETWEEN LEAST(30,20) AND GREATEST(30,20)

M
mletov, 2017-05-23
@mletov

Can be moved to scalar variables
Something like

DECLARE @minValue INT, @maxValue INT;
IF (@v1 < @v2) 
BEGIN
   SET @minValue = @v1;
   SET @maxValue = @v2;
END
ELSE
BEGIN
   SET @minValue = @v2;
   SET @maxValue = @v1;
END

SELECT *
FROM table
WHERE v BETWEEN @minValue AND @maxValue

Adjusted for MySql syntax

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question