2016-12-15 81 views
2

正常工作,我有三個表: em_employee:GROUP_CONCAT()是不是在MySQL

emp_number | emp_firstname | 
+------------+---------------+ 
|   1 | Vikram  | 
|   2 | S.   | 
|   3 | Gopal   | 
|   4 | Vaishnavi  | 
|   5 | Srinivasan | 
|   6 | Saravanan 

em_project

+------------+------------------------------+ 
| project_id | name       | 
+------------+------------------------------+ 
|  339 | MoneyGram     | 
|  340 | SERVICE LINE HEAD COMPLIANCE | 
|  341 | SERVICE LINE HEAD ANALYTICS | 
|  342 | GSI       | 
|  343 | Tandem      | 
|  344 | Master Card     | 
+------------+------------------------------+ 

em_project_employee:

+------------+------------+ 
| emp_number | project_id | 
+------------+------------+ 
|   1 |  339 | 
|   2 |  340 | 
|   3 |  341 | 
|   4 |  342 | 
|   1 |  343 | 
|   6 |  344 | 
|   2 |  342 | 
+------------+------------+ 

而且我要像輸出:

+------------+----------------------------------+ 
| emp_number | name        | 
+------------+----------------------------------+ 
|  1 | MoneyGram , Tandem    | 
|  2 | SERVICE LINE HEAD COMPLIANCE,GSI | 
|  3 | SERVICE LINE HEAD ANALYTICS  | 
|  4 | GSI        | 
|  6 | Master Card      | 
+------------+----------------------------------+ 

我已經用GROUP_CONCAT試過了,但是出了點問題。請幫助我。

+0

手段?我沒有得到。 – Madhura

+0

這意味着不要包含與您的問題無關的標籤,如'sql-server'。 – shmosel

+0

*但出現問題*您是否要告訴我們有什麼問題?或者你是否希望我們能猜出你所嘗試的以及問題所在。 – shmosel

回答

4

嘗試此查詢,它produes是輸出:

SELECT emp_number, GROUP_CONCAT(name) FROM em_project p 
INNER JOIN em_project_employee em ON p.project_id = em.project_id 
GROUP BY emp_number; 

數據的順序將E從什麼在你需要的輸出略有不同。如果排序很重要。

GROUP_CONCAT(name ORDER BY p.project_id) 
+0

非常感謝,它的工作很好。 – Madhura