mysql - 具有相同标识符的多行,如何通过多个左连接选择具有最高值的行(忽略其余行)?

标签 mysql sql left-join greatest-n-per-group

这个问题已经困扰我几个小时了。在发现我的旧查询有问题后,我不得不重建它。

情况:

我需要将每个 patient_id 与 clinic_id 匹配,为此我使用 patient_id 获取所有预约, 找到最高的 appointment_id 并使用其 clinic_id 设置最后一个已知的 clinic_id。

我的旧查询执行了此操作,但它跳过了从未预约过的患者。

这些是我当前的结果,但我需要过滤我的结果。问题是,如何?

+---------------+-------------------+-------------------+---------------+
|   patient_id  |   country_code    |   appointment_id  |   clinic_id   |
+---------------+-------------------+-------------------+---------------+
|       111     |       UK          |       620         |       3       |
|       111     |       UK          |       621         |       2       |
|       111     |       UK          |       1995        |       1       |
|       222     |       UK          |       609         |       3       |
|       222     |       UK          |       610         |       2       |   
|       333     |       UK          |       null        |       null    |
|       444     |       UK          |       null        |       null    |
+---------------+-------------------+-------------------+---------------+

我想要的是:

+---------------+-------------------+-------------------+---------------+
|   patient_id  |   country_code    |   appointment_id  |   clinic_id   |
+---------------+-------------------+-------------------+---------------+
|       111     |       UK          |       1995        |       1       |
|       222     |       UK          |       610         |       2       |     
|       333     |       UK          |       null        |       null    |
|       444     |       UK          |       null        |       null    |
+---------------+-------------------+-------------------+---------------+

我现在正在使用以下查询:

SELECT 
    patient.id,
    systemcountry.country_code,
    appointment_patient.appointment_id,
    appointment.clinic_id
FROM
    patient
        LEFT JOIN
    systemcountry ON patient.country_id = systemcountry.id
        LEFT JOIN
    appointment_patient ON patient_id = patient.id
        LEFT JOIN
    appointment ON appointment_patient.appointment_id = appointment.id

这是我的旧查询,有一个问题导致它跳过从未预约过的患者:

SELECT
    patient.id AS patient_id,
    systemcountry.code AS systemcountry_code,
    appointment.clinic_id
FROM
    patient
        LEFT JOIN
    systemcountry ON patient.land_id = systemcountry.id,
    appointment
WHERE
    appointment.id = (SELECT
            MAX(appointment_id)
        FROM
            appointment_patient
        WHERE
            patient_id = patient.id);

我还是个新手,请放轻松。

感谢任何意见,谢谢!

最佳答案

将原始查询的 WHERE 子句中的子选择移动到 LEFT JOIN(类似这样):

LEFT JOIN
    (SELECT MAX(appointment_id), patient_id
        FROM appointment_patient
        GROUP BY patient_id)  as apt ON patient.patient_id=apt.patient_id

关于mysql - 具有相同标识符的多行,如何通过多个左连接选择具有最高值的行(忽略其余行)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46977297/

相关文章:

php - MySql 查询到 Postgres 查询。 Postgres 不支持 IF 条件

php - 使用php在MySQL数据库中保存不同大小的双数组

mysql - 添加更多的 LEFT OUTER JOINS 会影响前面的列吗

php - 在 MySQL 中更新后不会回显消息

php - 使用 CURDATE() 按 DESC 排序;语句在 php mysql 中不起作用

sql - 如何获取 postgresql 9.5 中特定模式中存在的所有表的表行数?

mysql - 显示名称 mysql 中包含 "and"的发布者列表

sql - SQLite:根据其他列的条件将值除以一列

c# - 如果该行中的第一列为 null,则 Entity Framework 为该行返回 null

php - MySQL:如何根据另一个表中的可用性进行选择