我正在尝试查询列出所有客户,并获取最后的评论和 单个查询中的history_client表中该评论的日期,以便列出它。
select a.id_client,a.name,a.lastname,(select b.date_created,b.comentary
from history_of_client b where a.id_client = b.id_client_asociate) from clients_main_table
最佳答案
您可以在历史表上的 id_client 的 max(date_created) 上使用内部联接并联接
SELECT a.id_client,a.name,a.lastname, h.commentary
FROM clients_main_table a
INNER join (
select b.id_client_asociate, max(b.date_created) max_date
from history_of_client
group by b.id_client_asociate ) t on t.id_client_asociate = a.id_client
INNER JOIN history_of_client h on h.id_client_asociate = t.id_client_asociate
and h.date_created = t.max_date
关于mysql - 在选择中选择另一个表的第一条记录 - MYSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50337396/