How to search between columns in mysql

I have two columns in which values ​​(numbers) are stored, how to choose where my given number is between the values ​​in two columns?

Example

   `id | col1 | col2`  
    `1 | 20  | 50`  
    `2 | 200 | 400`  
    `3 | 500 | 650`

If I have a value of 25, how can I select the records where the value of 25 is between them, in this case it will be line 1

+3
source share
2 answers
select * from mytable where 25 between col1 and col2;
+10
source

You can try:

If you want to include col1 and col2 in your search:

select * from table where YOUR_NUM >= col1 and YOUR_NUM <= col2;

If not:

select * from table where YOUR_NUM > col1 and YOUR_NUM < col2;
+4
source

Source: https://habr.com/ru/post/1728571/


All Articles