MySQL LEFT JOIN 仅一行,按列排序,无子查询

MySQL LEFT JOIN only one row, ordered by column without subquery

是否可以在不使用子查询的情况下,只对其他 table 中按列(日期)排序的一行进行 LEFT JOIN。我的查询如下。可以用,但是超级慢。

SELECT * FROM clients c 
LEFT JOIN loan l ON c.id = l.id_client AND l.id = (
    SELECT id FROM loan ll
    WHERE ll.id_client = c.id
    ORDER BY `create_date` DESC
    LIMIT 1) 
GROUP BY k.id DESC 
ORDER BY c.register_date DESC 
LIMIT n , m; (n,m is from pagination)

有没有办法加快速度?

我将你的问题解释为 "Get me all loan details for the most recent loan for each client"

这应该可行...不过请注意假设。

SELECT * 
FROM 
    clients c 
    LEFT JOIN (select id_client, Max(id) id -- this assumes that a loan with a later create date will also have a higher id.
                from loan
                group by id_client) il
        on il.id_client = c.id
    inner join loan l
        on l.id = il.id
GROUP BY k.id DESC                          -- Dont know what "k" is
ORDER BY c.register_date DESC 
LIMIT n , m; (n,m is from pagination)