donglipi4495 2013-03-04 10:02
浏览 45
已采纳

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

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'

  • 写回答

2条回答 默认 最新

  • dongxie7683 2013-03-04 10:03
    关注

    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:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?