是否有可能只使用列(日期)排序的其他表中的一行进行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)
有没有办法加快速度?
答案 0 :(得分:2)
我将您的问题解释为“获取每位客户最近贷款的所有贷款详情”
这应该有用......但要注意这个假设。
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)