您可以在所有 SELECT 列(但不能在最后一列 - tpw.li_id_warnungen)上使用 GROUP BY,并将最后一列聚合到一个字段中。
PostgreSQL 解决方案:
您可以通过GROUP BY 和STRING_AGG 使用以下解决方案。要在STRING_AGG 上使用列tpw.li_id_warnungen,您必须将CAST 的值设置为TEXT。另一种不使用CAST 的解决方案是在列上使用直接强制转换tpw.li_id_warnungen::text。
SELECT
tp.tp_vorname AS PatientFirstName,
tp.tp_nachname AS PatientLastName,
tp.tp_geburtsdatum AS patientBirthday,
pt.pt_id,
te.te_startzeit AS StartTime,
te.te_endzeit AS EndTime,
te_datecreated AS DateCreated,
STRING_AGG(CAST(tpw.li_id_warnungen AS TEXT), ',') AS Warning
FROM
termin_patient tp INNER JOIN patienten_termin pt ON tp.tp_id = pt.tp_id
INNER JOIN termin te ON te.te_id = pt.pt_id
LEFT JOIN terminpatient_warnungen tpw ON tp.tp_id = tpw.tp_id
WHERE
pt.pt_id = te.te_id
AND tp.tp_id = 91168
AND te.te_startzeit >= '2017-05-24'
AND te.te_startzeit < '2017-06-02'
GROUP BY
tp.tp_vorname,
tp.tp_nachname,
tp.tp_geburtsdatum,
pt.pt_id,
te.te_endzeit,
te_datecreated
使用附加警告名称(在 cmets 中描述)您的查询将如下所示(不再需要 CAST 或直接转换):
SELECT
tp.tp_vorname AS PatientFirstName,
tp.tp_nachname AS PatientLastName,
tp.tp_geburtsdatum AS patientBirthday,
pt.pt_id,
te.te_startzeit AS StartTime,
te.te_endzeit AS EndTime,
te_datecreated AS DateCreated,
STRING_AGG(lip.li_name, ',') AS Warning
FROM
termin_patient tp INNER JOIN patienten_termin pt ON tp.tp_id = pt.tp_id
INNER JOIN termin te ON te.te_id = pt.pt_id
LEFT JOIN terminpatient_warnungen tpw ON tp.tp_id = tpw.tp_id
LEFT JOIN li_patientenwarnung lip ON tpw.li_id_warnungen = lip.li_id
WHERE
pt.pt_id = te.te_id
AND tp.tp_id = 91168
AND te.te_startzeit >= '2017-05-24'
AND te.te_startzeit < '2017-06-02'
GROUP BY
tp.tp_vorname,
tp.tp_nachname,
tp.tp_geburtsdatum,
pt.pt_id,
te.te_endzeit,
te_datecreated
MySQL 解决方案:
您可以使用GROUP BY 和GROUP_CONCAT 在MySQL 上使用以下解决方案:
SELECT
tp.tp_vorname AS PatientFirstName,
tp.tp_nachname AS PatientLastName,
tp.tp_geburtsdatum AS patientBirthday,
pt.pt_id,
te.te_startzeit AS StartTime,
te.te_endzeit AS EndTime,
te_datecreated AS DateCreated,
GROUP_CONCAT(tpw.li_id_warnungen) as Warning
FROM
termin_patient tp INNER JOIN patienten_termin pt ON tp.tp_id = pt.tp_id
INNER JOIN termin te ON te.te_id = pt.pt_id
LEFT JOIN terminpatient_warnungen tpw ON tp.tp_id = tpw.tp_id
LEFT JOIN li_patientenwarnung lip ON tpw.li_id_warnungen = lip.li_id
WHERE
pt.pt_id = te.te_id
AND tp.tp_id=91168
AND te.te_startzeit >= '2017-05-24'
AND te.te_startzeit < '2017-06-02'
GROUP BY
tp.tp_vorname,
tp.tp_nachname,
tp.tp_geburtsdatum,
pt.pt_id,
te.te_endzeit,
te_datecreated
使用附加警告名称(在 cmets 中描述),您的查询将如下所示:
SELECT
tp.tp_vorname AS PatientFirstName,
tp.tp_nachname AS PatientLastName,
tp.tp_geburtsdatum AS patientBirthday,
pt.pt_id,
te.te_startzeit AS StartTime,
te.te_endzeit AS EndTime,
te_datecreated AS DateCreated,
GROUP_CONCAT(lip.li_name) as Warning
FROM
termin_patient tp INNER JOIN patienten_termin pt ON tp.tp_id = pt.tp_id
INNER JOIN termin te ON te.te_id = pt.pt_id
LEFT JOIN terminpatient_warnungen tpw ON tp.tp_id = tpw.tp_id
WHERE
pt.pt_id = te.te_id
AND tp.tp_id=91168
AND te.te_startzeit >= '2017-05-24'
AND te.te_startzeit < '2017-06-02'
GROUP BY
tp.tp_vorname,
tp.tp_nachname,
tp.tp_geburtsdatum,
pt.pt_id,
te.te_endzeit,
te_datecreated