简体   繁体   中英

MySQL group_concat in subquery returns more than one row

I have the query, below, which should work. But, MySQL is giving me the error message

Subquery returns more than one row

SELECT e.episode_pk,
       (SELECT GROUP_CONCAT(d.fulldescription separator ', ')
        FROM episode_rhythm er
        JOIN diagnosis d ON er.diagnosis_fk = d.diagnosis_pk 
        WHERE er.episode_fk = e.episode_pk
        GROUP BY d.fulldescription) as rhythmDesc 
FROM episode e  
WHERE e.patientid_fk = '89976';

The entire purpose of using the GROUP_CONCAT in this query is to return only one row per 'episode'.

The outer select returns multiple rows.
The inner SELECT , when run for a single episode table primary key, returns a single row as you would suspect:

SELECT group_concat(d.fulldescription separator ', ')
FROM episode_rhythm er
JOIN diagnosis d ON er.diagnosis_fk = d.diagnosis_pk 
WHERE er.episode_fk = 234776
GROUP BY d.fulldescription;

The inner SELECT without the GROUP_CONCAT , when run for a single episode table primary key, may return multiple rows:

SELECT d.fulldescription
FROM episode_rhythm er
JOIN diagnosis d ON er.diagnosis_fk = d.diagnosis_pk 
WHERE er.episode_fk = 234776;

What do I need to do to get this to work as I would expect?

Your subquery within the SELECT is a bit doubtful.

I guess what you're looking for is:

SELECT
    e.episode_pk,
    GROUP_CONCAT(d.fulldescription separator ', ')
FROM episode_rhythm er
JOIN diagnosis d ON er.diagnosis_fk = d.diagnosis_pk
JOIN episode e ON er.episode_fk = e.episode_pk
WHERE e.patientid_fk='89976' 
GROUP BY e.episode_pk

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