mysql - Postgresql 合并来自 JOIN 的相同列

标签 mysql sql postgresql join

我提出的问题有点傻。但是让我用一个我正在努力解决的例子来表达我的问题。

我的查询给出了以下结果: enter image description here

如您所见,除最后一列外,其他内容均相同。我怎样才能将结果加入一行,最后一列的两个值。

我的查询:

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 BYSTRING_AGG 使用以下解决方案.要在 STRING_AGG 上使用列 tpw.li_id_warnungen,您必须将值CASTTEXT。另一种不使用 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 BYGROUP_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/

相关文章:

sql - 根据postgresql中的组角色过滤查询行

mysql - 如何在 MySQL 的 SELECT 语句中正确添加附加列?

postgresql - 函数返回 "query has no destination for result data"

mysql - 来自 Sql 数据库的简单随机样本

MYSQL 多个条件语句进行计数

php - 根据 db 中的值在 wp 页脚中动态显示代码

php - 有没有更安全的方法来使用 PDO 来避免不必要的查询?

SQL - 将所有用户合并到一张表中

MySQL 查询时间太长,未使用索引

postgresql - 缓慢的 Postgres 9.3 查询,再次