tags:

views:

96

answers:

2

Hi,

I'm struggling with some logic here.

In my MySQL database I have a table that looks like this:

id | lower | upper
1  | 0     | 120
2  | 121   | 200
3  | 201   | 500

Now, my form posts two values e.g. 121 and 300.

What query can get those rows where 121 to 300 overlap the lower and upper columns?

In this example these rows are: 2 and 3

I just can't figure it out..

A: 
SELECT * FROM Table1
WHERE lower <= 300 AND upper >= 121
Mark Byers
That's not what I ment because this only covers if the posted range is BETWEEN the upper and lower values, the posted range isn't allowed to overlap the lower and upper range..
Bundy
No, that's not what this query does. Did you even try it? It fetches overlaps, not between. Between would be "WHERE lower >= 121 AND upper <= 300".
Mark Byers
A: 
SELECT * 
FROM table 
WHERE (121 <= lower AND 300 >= lower) OR (121 <= upper AND 300 >= upper)

This works perfectly

Bundy