tags:

views:

31

answers:

2

Hi, i have two columns that store values(numbers), how do i select where my given number is between the values in the 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 records where the value of 25 is between them which in this case would be row 1

+3  A: 

You can try:

If you want to include col1 and col2 in the 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;
codaddict
+4  A: 
select * from mytable where 25 between col1 and col2;
Asaph
Be sure to use EXPLAIN to check that the optimiser will use an appropriate index for this; it should ideally be able to range scan either col1 or col2
MarkR