I have 2 tables 'users' and 'criteria'.
users
------------------------------
username, age, height, country
------------------------------
criteria
-----------------------------------------------------------
username, age_from, age_to, height_from, height_to, country
-----------------------------------------------------------
I want to write a query that 'users' age between 'age_from' and 'age_to'
AND
'users' 'height' between 'height_from' and 'height_to'
JOIN
the two tables:
SELECT u.*
FROM Users AS u
INNER JOIN criteria AS c ON u.hiehgt BETWEEN c.height_from
AND c.height_to
AND u.age BETWEEN c.age_from
AND c.age_to;
You might also need to use OUTER JOIN
instead of INNER JOIN
to get those unmatched rows, see this for more information:
Try:
SELECT u.*
FROM users u
INNER JOIN criteria c
WHERE u.age BETWEEN c.age_from AND c.age_to
AND u.height BETWEEN c.height_from AND c.height_to;