简体   繁体   中英

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. There are two records in expo_session_speaker that correspond to session_id 169 - ie there are two speakers for this one session.

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:

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?

EDITS FOR @NOAH

Main table is expo_session_eval - each record has a session_id stored. These IDs come from a table called expo_session. There's a cross-reference table called expo_session_speaker that contains only two columns - session_id and speaker_id. There's another table called speaker_id that contains speaker_id and people_id. people_id links the table to a table called new_people, which contains first_name and 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. 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; 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.

(Clear as mud, right?)

You should group by session_id (instead of ordering)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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