1

在 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
4

8 回答 8

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'
于 2013-06-22T07:15:38.220 回答
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'
于 2013-06-22T07:12:41.943 回答
3

尝试以下操作:

Select * from profile natural join country where fullname='Cindy'
于 2013-06-22T07:20:48.653 回答
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";
于 2013-06-22T07:14:04.157 回答
2

你应该使用JOIN

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

于 2013-06-22T07:14:32.570 回答
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';
于 2013-06-22T07:15:18.887 回答
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'
于 2013-06-22T07:17:00.773 回答
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'
于 2017-08-22T06:40:43.710 回答