2012-11-29 2 views
2

아무도 도와 드릴 수 있습니까?
country_id로 연결된 2 개의 테이블 country_table 및 language_table에서 쿼리하고 있습니다.php mysql은 concat이있는 테이블에 합류합니다

country_table :

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

language_table :

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 ---- 
+0

, 젊은 황새치를 참조하십시오? – Madbreaks

+0

내가 한 일은 국가 목록을위한 while 루프 쿼리 안에 또 다른 쿼리 (언어)를 삽입 한 것이고, 나는 그 나쁜 생각을 알고있다. 바로 그것을 고치기 위해 도움을 구하고있다. – swordfish

+0

정보 및 일부 코드를 추가하십시오! –

답변

3

다음 수행 할 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 with Demo 시도 무엇

+0

case 문과 함께 잘 작동하면 거기에 있습니다. – bikedorkseattle

+0

안녕하세요, bluefeet, 답변이 작동 중입니다, 그냥 후속 질문, 언어에 대한 선택기는 무엇입니까? 샘플, $ row [ " ']? – swordfish

+0

그 구문에 익숙하지 않습니다. 다른 도움을 얻으려면 다른 질문을 게시하고 싶을 수도 있습니다. – Taryn

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