如何根据另一个表列从一个表中选择一列?

时间:2013-03-04 10:02:38

标签: php mysql sql select join

我有2个表'用户'和'标准'。

用户

------------------------------
username, age, height, country
------------------------------

标准

-----------------------------------------------------------
username, age_from, age_to, height_from, height_to, country
-----------------------------------------------------------

我想写一个'用户'年龄介于'age_from'和'age_to'之间的查询

'height_from'和'height_to'之间的'用户''身高'

2 个答案:

答案 0 :(得分:2)

JOIN两个表:

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;

您可能还需要使用OUTER JOIN代替INNER JOIN来获取这些不匹配的行,有关详细信息,请参阅此处:

答案 1 :(得分:0)

尝试:

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;