在Select-MYSQL中选择另一个表的第一条记录
我试图做一个查询,列出所有客户,并得到最后的评论和建议 在要列出的单个查询中,该注释在history_client表中的日期在Select-MYSQL中选择另一个表的第一条记录,mysql,Mysql,我试图做一个查询,列出所有客户,并得到最后的评论和建议 在要列出的单个查询中,该注释在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 使用左连接和内连接来获得所需的结果集 sel
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
使用左连接和内连接来获得所需的结果集
select a.id_client,
a.name,
a.lastname,
hc.date_created,
hc.comentary
from clients_main_table c
left join (select id_client_asociate,max(date_created) dt from history_of_client group by id_client_asociate) h
on (c.id_client = b.id_client_asociate)
inner join history_of_client hc
on (hc.id_client_asociate = b.id_client_asociate and hc.date_created = h.date_created)
您可以在历史表上的id\u客户机的max(date\u 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
你能更新样本数据和预期输出吗?特别是我最希望得到的不是客户端历史记录的最后日期,我也想得到最后日期的结果,谢谢。看到了吗?左连接内部连接和内部连接内部连接不一样吗?我添加了内部连接,因为有问题,他/她添加了一个内联列,因此可能会有一些记录没有匹配的记录。我不知道这将如何工作-但我还没有喝任何咖啡,所以可能我只是错过了一些东西