2011-01-28 42 views
0

謝謝大家你幫我的第一次嘗試再次卡住 - 需要另一個GROUP_CONCAT添加到MySQL查詢

我想第二GROUP_CONCAT添加到查詢到的數據返回到VB.Net數據網格。我想彙總2列到新列有點像下面的結果:

現有數據

Date  Sponsor Match_no  Team  
-------------------------------------------- 
1-1-11  Nike  1   Tigers  
1-1-11  Nike  1   Bears 
2-1-11  Crisco  2   Llamas 
2-1-11  Crisco  2   Tigers 


Date  Sponsor Match_no  Tags  
    -------------------------------------------- 
    1-1-11  Nike  1   Away 
    1-1-11  Nike  1   Rained out 
    2-1-11  Crisco  2   Home 
    2-1-11  Crisco  2   Injury 

並推出兩列了GROUP_CONCATs得到的結果是這樣的

Date  Sponsor Match_no  Teams_playing   Tags 
    ---------------------------------------------------------------- 
    1-1-11 Nike   1   Tigers vs Bears  Away, Rained Out 
    2-1-11 Crisco  2   Llamas vs Tigers  Home, injury 

我在這裏接受了一些建議,並在Team_matches和Matches_tags上創建了連接表。

現在有7個表格:

Dates  Sponsors  Match  Team   Tags matches_tags team_matches 
-------------------------------------------------------------------------------------- 
Date_id  Sponsor_id  Match_id  Team_id Tag_id  Match_id  Team_id 
Date  Sponsor_name Match_no  Team_name Tag_name Tag_id  Match_id 
      date_id  sponsor_id 

我的查詢到目前爲止是:

select d.date, s.sponsor_name, m.match_no, 
group_concat(t.team_name separator ' vs ') Teams_playing, 
group_concat(tg.tag_name separator ' , ') Comments 
from matchs m 
inner join matches_teams mte on mte.match_id = m.match_id 
inner join matches_tags mta on mta.match_id = m.match_id 
inner join team t on t.team_id = mte.team_id 
inner join tags tg on tg.tag_id = mta.tag_id 
inner join sponsors s on s.sponsor_id = m.sponsor_id 
inner join dates d on d.date_id = s.date_id 
group by m.match_id, d.date, s.sponsor_name, m.match_no, tg.tag_id 

並返回結果:

date sponsor  match_no  teams playing comments 
-------------------------------------------------------------------- 
1-1-11 Nike   1   Bears vs Tigers Rained out , Rained out 
1-1-11 Nike   1   Bears vs Tigers Cancelled , Cancelled 
1-1-11 Nike   3   Earwigs vs Goombas Away , Away 
2-1-11 Crisco   2   Tigers vs Llamas Away , Away 

這不是什麼即時通訊後:

+0

等待,什麼體育賽事具有克里斯科作爲贊助商? – SeanDowney 2012-05-24 19:31:00

回答

0
SELECT d.date, s.sponsor_name, m.match_no, 
     GROUP_CONCAT(t.team_name separator ' vs ') Teams_playing, 
     (
     SELECT GROUP_CONCAT(tg.tag_name SEPARATOR ', ') 
     FROM matches_tags mta 
     JOIN tags tg 
     ON  tg.tag_id = mta.tag_id 
     WHERE mta.match_id = m.match_id 
     ) AS comments 
FROM matchs m 
JOIN matches_teams mte on mte.match_id = m.match_id 
JOIN team t on t.team_id = mte.team_id 
JOIN sponsors s on s.sponsor_id = m.sponsor_id 
JOIN dates d on d.date_id = s.date_id 
GROUP BY 
     m.match_id 
+0

感謝您的快速回復Quassnoi - 那關閉了,但它返回錯誤的標籤值1-1-11,耐克,1,熊與老虎,外出,取消,離開, 2-1-11,Crisco,2, 1-1-11,Nike,3,Earwigs vs Goombas,Rained out,Canceled,Away,Away 比賽1的標籤應該「下雨,取消」和匹配2應該是「走開」 – dMO 2011-01-28 17:37:24