简体   繁体   English

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

[英]how to select a column from one table according to another table column?

I have 2 tables 'users' and 'criteria'. 我有2个表'用户'和'标准'。

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' 我想写一个'用户'年龄介于'age_from'和'age_to'之间的查询

AND

'users' 'height' between 'height_from' and 'height_to' 'height_from'和'height_to'之间的'用户''身高'

JOIN the two tables: 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;

You might also need to use OUTER JOIN instead of INNER JOIN to get those unmatched rows, see this for more information: 您可能还需要使用OUTER JOIN而不是INNER JOIN来获取那些不匹配的行,有关详细信息,请参阅此处:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从一个表中选择与表中的列值相匹配的数据? - How do I select data from one table where column values from that table match the conatenated column values of another table? 如何从与另一个表具有一对多关系的表中选择特定列? - How to select specific column from a table that has one to many relationship with another table? 如何将每列中的 2 个值输入到另一个表中的一列 - how to input 2 value from each column to one column in another table MySQL-如何从另一个表控制一个表的“数字”列 - MySQL - How to control `numeric` column of one table from another table MySql中如何将列数据从一张表复制到另一张表? - How to Copy Column data from one table to another table in MySql? SQL - 将数据从一个表列复制到另一个表列 - SQL - copy data from one table column to another table column 将列从一个表连接到另一个表CodeIgniter中的另一列 - Join column from one table to another column in other table CodeIgniter Laravel 4从子查询中的另一个表中选择列 - Laravel 4 select column from another table in subquery 如何根据MySQL中的另一个条件有条件地选择列 - How to select column conditionally according to another one in MySQL 从一个表中选择记录,并使用另一表中的列对结果进行排序 - Select records from one table and sort the result using a column from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM