Selecting data with a range condition on two columns
mysql
Solution
Flip the logic:
WHERE age_from <= 22
AND age_to >= 19
My favourite explanation of this kind of problem, courtesy of Rudy Limeback (aka r937): http://www.dbforums.com/6318776-post14.html
Problem
i have some articles in my database ``` +------+--------------+----------+--------+ | id | article_name | age_from | age_to | +------+--------------+----------+--------+ | 1337 | article 1 | 30 | 60 | +------+--------------+----------+--------+ | 1338 | article 2 | 16 | 35 | +------+--------------+----------+--------+ | 1338 | article 3 | 26 | 28 | +------+--------------+----------+--------+ ``` The user can set some filters in the front-end. He can search articles that are made for people from 19 years to 22 years. There are also two input fields (Age from and age to). The database should return this: ``` +------+--------------+----------+--------+ | id | article_name | age_from | age_to | +------+--------------+----------+--------+ | 1338 | article 2 | 16 | 35 | +------+--------------+----------+--------+ ``` How do i do that? i can't do it with `WHERE age_from >= 19 AND age_to <= 22`. greetings