2

谁能帮我这个?
我正在查询由 country_id 链接的 2 个表 country_table 和 language_table。

国家表:

country_id    continent      country  
 100          asia            china
 101          asia            japan
 102          europe          UK
 103          europe          germany  

语言表:

country_id    language_id     language
 100             01           mandarin  
 100             02           cantonese  
 102             03           english  
 102             04           french
 102             05           welsh          

我想要实现的是显示所有有或没有语言的国家。如果它有语言,它应该像下面的示例输出一样连接起来。

continent    country    language  
asia          china      mandarin, cantonese  
asia          japan      ----
europe        UK         english, french, welsh
europe        germany    ----  
4

2 回答 2

4

您可以使用该GROUP_CONCAT()功能执行以下操作:

select c.continent,
  c.country,
  group_concat(case 
               when l.language is null 
               then '----' else l.language end order by l.language) language
from country_table c
left join language_table l
  on c.country_id = l.country_id
group by c.continent, c.country

请参阅带有演示的 SQL Fiddle

于 2012-11-29T23:31:08.283 回答
0

你需要做这样的事情。

SELECT ct.continent, ct.country, GROUP_CONCAT(lt.language)
FROM country_table ct
LEFT JOIN language_tabel lt USING(country_id)
GROUP BY ct.country
于 2012-11-29T23:30:03.533 回答