简体   繁体   English

MYSQL SELECT WHERE子句:如何包含另一个表中的列?

[英]MYSQL SELECT WHERE Clause : How to include a column from another table?

In MYSQL, let's say I have following two tables. 在MYSQL中,假设我有以下两个表。

"profile": “轮廓”:

fullname | gender | country_code
-----------------------------------
Alex     | M      | us
Benny    | M      | fr
Cindy    | F      | uk

"country": “国家”:

country_code | country_name
-----------------------------
jp           | Japan
us           | United States of America
fr           | France
sg           | Singapore
uk           | United Kingdom

While querying from "profile" table's perspective, like this: "profile"表的角度进行查询时,如下所示:

WHERE fullname = 'Cindy'

Then in the result, how do i include a column from another Table (to get the results like this below): 然后在结果中,我如何从另一个表中包含一列(以获取如下所示的结果):

fullname | gender | country_code | country_name
------------------------------------------------
Cindy    | F      | uk           | United Kingdom

You can use 您可以使用

select a.fullname, a.gender, b.country_code, b.country_name 
FROM profile a 
LEFT JOIN country b ON a.country_code = b.country_code 
WHERE a.fullname='Cindy'

You need to JOIN the tables. 您需要加入表。 For example: 例如:

select a.fullname, a.gender, b.country_code, b.country_name 
  from profile a JOIN country b 
    on a.country_code = b.country_code 
 where a.fullname='Cindy'

请尝试以下操作:

Select * from profile natural join country where fullname='Cindy'
 select fullname, gender, profile.country_code as country_code, country_name from profile join country on profile. country_code = profile.country_code where fullname = "Cindy";

You should use JOIN : 您应该使用JOIN

SELECT profile.*, country.country_name
FROM Customers
INNER JOIN Orders
ON profile.country_code=country.country_code

For more info check this : http://www.w3schools.com/sql/sql_join_inner.asp 有关更多信息,请检查: http : //www.w3schools.com/sql/sql_join_inner.asp

Try this.. 尝试这个..

SELECT t1.fullname, t1.gender t1.country_code,t2.country_name
FROM profile AS t1 INNER JOIN country AS t2 ON t1.country_code = t2.country_code where t1.fullname='cindy';
select p.fullname,p.gender,p.country_code,c.country_name from profile p 
INNER JOIN country c on p.country_code=c.country_code where p.fullname='Cindy'

You need to use join between profile and country table as below 您需要使用个人资料和国家/地区表之间的联接,如下所示

SELECT
profile.fullname,
profile.gender, 
country .country_code, 
country .country_name 
FROM profile as profile JOIN country as country 
       ON (profile.country_code = country.country_code)
  WHERE profile.fullname = 'Cindy'

暂无
暂无

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

相关问题 如何获取另一个表的行数,使用 SELECT 列作为使用 MySQL 的 WHERE 子句 - How to get a row count of another table, using SELECT column as a WHERE clause using MySQL MySQL:如何从一个表中选择和显示所有行,并计算另一个表中where子句的总和? - MySQL: How to select and display ALL rows from one table, and calculate the sum of a where clause on another table? 如何使用另一个表中的where子句查询MySql表? - How to query a MySql table with a where clause from another table? mysql-从带有sum()和where子句的表中选择 - mysql - select from a table with sum() and where clause 使用where子句从另一个表中选择数据 - Select data from another table with where clause MySQL多插入从另一个表中选择多列 - mysql multiple Insert where select many column from another table MYSQL仅在该表的列值与另一表的列值匹配的情况下,才如何从该表中选择数据? - MYSQL How do I select data from one table only where column values from that table match the column values of another table? Select from table where 子句 from array from another table - Select from table where clause from array from another table 如何从另一个表中不存在列的表中进行选择 - How to select from a table where column not exists in another table mysql在where子句中从同一表中选择两次具有不同日期的列 - mysql Select one column twice from the same table with different dates in the where clause
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM