5

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?

informatik01
  • 16,038
  • 10
  • 74
  • 104
user1469297
  • 101
  • 1
  • 2
  • 4

1 Answers1

4

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
Olivier Coilland
  • 3,088
  • 16
  • 20