我提出的问题有点傻。但是让我用一个我正在努力解决的例子来表达我的问题。
如您所见,除最后一列外,其他内容均相同。我怎样才能将结果加入一行,最后一列的两个值。
我的查询:
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, 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
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'
最佳答案
您可以在所有 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
使用额外的警告名称(在注释中描述),您的查询将如下所示(不再需要 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
使用额外的警告名称(在注释中描述),您的查询将如下所示:
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
关于mysql - Postgresql 合并来自 JOIN 的相同列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44673165/