我有下面的查询,应该可以使用。但是,MySQL给了我错误消息'Subquery返回多行'。
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'
在此查询中使用group_concat的唯一目的是每个'episode'只返回一行。
外部选择返回多行。
内部选择,当针对单个剧集表主键运行时,会返回您怀疑的单行:
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
没有group_concat的内部选择,当为单个剧集表主键运行时,可能会返回多行:
select d.fulldescription
from episode_rhythm er join diagnosis d on er.diagnosis_fk = d.diagnosis_pk
WHERE er.episode_fk = 234776
我需要做些什么才能让它按照我的预期运作?
答案 0 :(得分:1)
SELECT中的子查询有点令人怀疑。
我想你要找的是:
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