简体   繁体   English

GROUP BY和GROUP_CONCAT-如何返回正确的值?

[英]GROUP BY and GROUP_CONCAT - how to return correct values?

I have the following query: 我有以下查询:

SELECT ev.q1, ev.comments, es.session_number, es.title, CONCAT( np.first_name,  ' ', np.last_name ) AS speaker
FROM  `expo_session_eval` ev
LEFT JOIN expo_session es ON es.session_id = ev.session_id
LEFT JOIN expo_session_speaker ess ON ess.session_id = ev.session_id
LEFT JOIN expo_speaker sp ON sp.speaker_id = ess.speaker_id
LEFT JOIN new_people np ON np.id = sp.people_id
GROUP BY CONCAT( np.first_name,  ' ', np.last_name ) 
ORDER BY es.session_number

This returns data that looks like this: 这将返回如下所示的数据:

session_id  q1  comments    session_number  title               speaker
===================================================================================
169         3   Good!       103            Digital Practices      Steve Bullock
169         3   Good!       103            Digital Practices      Sheila Bacon
170         1               104            LBS = Location Based   Patrick Moorhead

This is correct in that there are two records in expo_session_eval for session_id 169, but it's incorrect in that the q1 and comments values are not identical. 这是正确的,因为expo_session_eval中有两个session_id 169记录,但是这是不正确的,因为q1和注释值不相同。 There are two records in expo_session_speaker that correspond to session_id 169 - ie there are two speakers for this one session. expo_session_speaker中有两个记录与session_id 169相对应-即该会话有两个发言者。

Ideally, I'd like my results to look like this: 理想情况下,我希望结果如下所示:

session_id  q1  comments    session_number  title                  speaker
===============================================================================================
169         3   Good!       103            Digital Practices        Steve Bullock, Sheila Bacon
169         5   Great!      103            Digital Practices        Steve Bullock, Sheila Bacon
170         1               104            LBS = Location Based     Patrick Moorhead

I've tried using GROUP_CONCAT, but apparently I'm not using it correctly, because when I use this query: 我尝试使用GROUP_CONCAT,但是显然我没有正确使用它,因为当我使用此查询时:

SELECT ev.session_id, ev.q1, ev.comments, es.session_number, es.title, GROUP_CONCAT( CONCAT( np.first_name,  ' ', np.last_name ) ) AS speaker
FROM  `expo_session_eval` ev
LEFT JOIN expo_session es ON es.session_id = ev.session_id
LEFT JOIN expo_session_speaker ess ON ess.session_id = ev.session_id
LEFT JOIN expo_speaker sp ON sp.speaker_id = ess.speaker_id
LEFT JOIN new_people np ON np.id = sp.people_id
ORDER BY es.session_number

I get this: 我得到这个:

session_id  q1  comments    session_number  title                  speaker
===============================================================================================
169         3   Good!       103         Digital Practices   Steve Bullock,Sheila Bacon,Patrick Moorhead

What do I need to do to make the speaker column group_concat within the session_id? 我需要怎么做才能使session_id中的发言人列group_concat?

EDITS FOR @NOAH @NOAH的编辑

Main table is expo_session_eval - each record has a session_id stored. 主表是expo_session_eval-每个记录都存储有一个session_id。 These IDs come from a table called expo_session. 这些ID来自称为expo_session的表。 There's a cross-reference table called expo_session_speaker that contains only two columns - session_id and speaker_id. 有一个名为expo_session_speaker的交叉引用表,该表仅包含两列-session_id和Speaker_id。 There's another table called speaker_id that contains speaker_id and people_id. 还有一个名为Speaker_id的表,其中包含Speaker_id和people_id。 people_id links the table to a table called new_people, which contains first_name and last_name. people_id将表链接到名为new_people的表,该表包含first_name和last_name。

In expo_session_speaker, there can be multiple records for a given session_id - each record corresponds to a single session and a single speaker. 在expo_session_speaker中,给定session_id可以有多个记录-每个记录对应一个会话和一个发言人。 So to display ALL speakers for a given session, you end up with multiple records. 因此,要显示给定会话的所有发言人,您将获得多个记录。 That's where the group by/group_concat idea came in; 这就是group by / group_concat创意的来源; I need to display ALL the speakers for a given session in ALL records for that session. 我需要在该会话的所有记录中显示给定会话的所有发言人。 Hence, session 169 record 1 speakers are Steve Bullock, Sheila Bacon, and session 169 record 2 speakers are the same, even though none of the other selected values will be the same for the two session 169 records. 因此,会话169记录1的演讲者是史蒂夫·布洛克(Steve Bullock),希拉·培根(Sheila Bacon),会话169记录2的演讲者是相同的,即使两个会话169记录的其他选定值都不相同。

(Clear as mud, right?) (清澈见底吧?)

You should group by session_id (instead of ordering) 您应该按session_id分组(而不是排序)

SELECT ev.session_id, ev.q1, ev.comments, es.session_number, es.title, GROUP_CONCAT(  CONCAT( np.first_name,  ' ', np.last_name ) ) AS speaker
FROM  `expo_session_eval` ev
LEFT JOIN expo_session es ON es.session_id = ev.session_id
LEFT JOIN expo_session_speaker ess ON ess.session_id = ev.session_id
LEFT JOIN expo_speaker sp ON sp.speaker_id = ess.speaker_id
LEFT JOIN new_people np ON np.id = sp.people_id
GROUP BY ev.session_id

Something like this should do what you want - 这样的事情应该做你想要的-

SELECT ev.session_id, ev.q1, ev.comments, es.session_number, es.title, speaker
FROM  `expo_session_eval` ev
LEFT JOIN expo_session es ON es.session_id = ev.session_id
LEFT JOIN (
    SELECT ess.session_id, GROUP_CONCAT( CONCAT( np.first_name,  ' ', np.last_name ) ) AS speaker
    FROM new_people np
    LEFT JOIN expo_speaker sp ON  np.id = sp.people_id
    LEFT JOIN expo_session_speaker ess ON sp.speaker_id = ess.speaker_id
    GROUP BY ess.session_id
) np ON np.session_id = es.session_id   
ORDER BY es.session_number

You don't actually want to use a GROUP BY statement on the entire result set, just to create a concatenated string of the speakers so it needs to be done in a subquery. 您实际上并不想在整个结果集上使用GROUP BY语句,而只是创建发言人的串联字符串,因此需要在子查询中完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM