再次卡住 - 需要向 MySQL 查询添加另一个 GROUP_CONCAT

发布于 2024-10-14 12:34:28 字数 2568 浏览 4 评论 0原文

感谢大家对我第一次尝试的帮助,

我正在尝试将第二个 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

这不是我之后的结果:

Thank you all for you help on my first attempt

I am trying to add a second group_concat to a query to return data to a VB.Net datagrid. I want to rollup the results of 2 columns into new columns kinda like below:

Existing data

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

and roll both columns up with GROUP_CONCATs to get a result something like this

 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

I took someone advice here and created join tables on Team_matches and Matches_tags

Now there is 7 tables :

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 

My query so far is:

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

and returns results:

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

Which is not what im after:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如何视而不见 2024-10-21 12:34:28
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文