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

时间:2013-06-22 07:09:09

标签: mysql select include

在MYSQL中,假设我有两个表格。

“轮廓”:

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

“国家”:

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

"profile"表的角度查询,如下:

WHERE fullname = 'Cindy'

然后在结果中,我如何包含另一个表中的列(以获得如下所示的结果):

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

8 个答案:

答案 0 :(得分:5)

您可以使用

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'

答案 1 :(得分:3)

您需要加入表格。例如:

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'

答案 2 :(得分:3)

尝试以下方法:

Select * from profile natural join country where fullname='Cindy'

答案 3 :(得分:2)

 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";

答案 4 :(得分:2)

您应该使用加入

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

有关详情,请查看:http://www.w3schools.com/sql/sql_join_inner.asp

答案 5 :(得分:2)

试试这个..

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';

答案 6 :(得分:2)

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'

答案 7 :(得分:1)

您需要在个人资料和国家/地区表格之间使用联接,如下所示

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'