2009-12-18 207 views
2

說,還有2個表:在SQL中列舉枚舉?

Person 
| id | name | 

LanguagesSpoken 
| person_id | language_name | 

有沒有一種方法來創建將包含2列的觀點:person_namelanguages_spoken作爲一個逗號分隔的列表?

我正在開發針對SQLite。

回答

3

用途:

SELECT p.name, 
     GROUP_CONCAT(ls.language_name, ',') 
    FROM PERSON p 
    JOIN LANGUAGESSPOKEN ls ON ls.personid = p.personid 
GROUP BY p.name 

參考:SQLite aggregate functions

2
select name, group_concat(language_name) as languages 
from person p inner join languagesSpoken l 
on p.id = l.person_id 
group by l.person.id 
1

GROUP_CONCAT()是你的答案。