2017-05-29 37 views
0

我有一個行業和名稱列的人員表。我可以通過使用以下查詢獲得基於行業的值在mysql表中有不同值的重複記錄

SELECT * 
FROM `people` 
ORDER BY `industry` ASC, `name` ASC 

在我的表中有相同行業的記錄(人員)。是否有可能讓他們獲得相同的產出? 對於現在的查詢返回此

industry1 
------ 
name1 

industry1 
------ 
name2 

industry2 
------ 
name 
---------------------------------------------------- 

我怎麼能有它這樣?

industry1 
------ 
name1 
name2 

industry2 
------ 
name 

var num = 0; 
 
    for(var i = 0;i < r.length; i++) { 
 
    var country = r[i].country; 
 
    if(i < r.length-1) nextCountry = r[i+1].country; 
 
    
 
    
 
    
 
    if(lastCountry != country && nextCountry == country) { 
 
     num = 0; 
 
    } 
 
    %> 
 
    
 
    <div class="<%= lastCountry %> <%= country %> <%= nextCountry %>"></div> 
 
     <div class="contact-list"> 
 
     <div class="contact-info contact-padding"> 
 
      <% if(r[i].industry) { %> <h2 class="h-style"><%= r[i].industry %></h2> <% }%> 
 
      <span class="name"><%= r[i].name %> <% if(r[i].segment) { %>&#40;<%= r[i].segment %>&#41;<% }%></span> 
 
      <span class="phone"><%= r[i].phone %></span> 
 
      <span class="email"><%@ "generic_email" %></span> 
 
     </div> 
 
     </div> 
 
    
 
    <% 
 
    \t num++; 
 
    var lastCountry = country; 
 
    } 
 

 
    %>

這是認爲我建立和消耗的數據,簡單地重複爲每個記錄的代碼相同的塊。如果我嘗試使用GROUP BY,它只返回

ONE OF THE INDUSTRIES 

name1, name2,name3,name4, ... 
phone number 
email 
+0

意味着什麼? –

+0

@FahadAnjum是的,具有相同行業標籤的人應該組合在一列 – k185

+0

認真考慮處理應用程序代碼 – Strawberry

回答

0

如果我理解您的要求顯然,你需要像

SELECT `industry`,GROUP_CONCAT(`name`) as name 
FROM `people` 
GROUP BY `industry` 
ORDER BY `industry` ASC 

這會給你每industryname通過comma級聯1行。如果你想要不同的separator像管道或空間,使用GROUP_CONCAT(name SEPARATOR <custom separator>)

+0

中的數據顯示問題GROUP_CONCAT,將所有行中的所有名稱連成一行。它消除了所有其他行業,只保留最後一個行業。因此,我只有一個行業名下的所有名稱。沒有幫助 – k185

+0

'它消除了所有其他行業,只保留最後一個'=>當你使用'group by'時,沒有什麼叫'last'或'first'。你會得到所有'獨特'行業的行,例如'industry1'' industry2'。在問題中以表格格式添加樣本數據和預期輸出。 – Utsav

+0

我更新了視圖 – k185

0

只需使用GROUP BY這兩列。

SELECT * FROM 人 GROUP BY行業,名稱 ORDER BY industry ASC,ASC name要在一個欄,顯示的結果