Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter set of numbers between 2 columns?

Tags:

sql

mysql

I have a table that has min and max columns. I want to search for rows where a set of numbers are between this range of min and max.

Example: I have a set (3, 4, 11, 18).

  1. My query filtering could look:

    (3 between min and max) OR
    (4 between min and max) OR
    (11 between min and max) OR
    (18 between min and max)
    
  2. But I wanted to know if there is a better way something like:

    (3,4,11,12) between min and max
    

    that will do similar to #1

I need this because the set can change between different searches. Any help or suggestions are appreciated.

The table in simpler context is class (classid, classname, minage, maxage). There are thousands of classes - so I get web request searching for classes with specific set of ages for example (3,4,11,12) where a user is searching for classes for ages 3, 4, 11 and 12.

Currently my query looks: select * from class where ((3 between min and max) OR (4 between min and max) OR (11 between min and max) OR (18 between min and max))

like image 766
chan Avatar asked Jan 04 '12 13:01

chan


1 Answers

Something like

SELECT * 
  FROM MyTable AS T
 WHERE EXISTS (
               SELECT *
                 FROM MySet AS S
                WHERE S.val BETWEEN T.my_min AND T.my_max
              );
like image 79
onedaywhen Avatar answered Oct 19 '22 02:10

onedaywhen