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
解决方案
您可以在 max(date_created) 上为历史表上的 id_client 使用内部连接并加入
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
推荐阅读
- javascript - 如何通过多个属性过滤对象数组?
- html - 当我从计算机中删除图像时,我在 html 中使用了图像标签并插入了图像路径,我看不到图像,为什么?
- ethereum - Solidity - ERC20 法律术语
- ruby-on-rails - 热线断开 link_to rails
- python-3.x - OpenCV的绘制矩形(cv2.rectangle)中颜色参数的python绑定发生了什么?
- dart - Dart :: Process.run() 将标准输出作为流而不是字符串,就像 Process.start()
- volttron - volttron 日志文件管理最佳实践
- ubuntu-18.04 - 邮件服务器无法将我的邮件识别为有效发件人
- c - cs50, int rank 和 voter 在哪里增加?
- spyder - 在 spyder 中保存时,有没有办法阻止文件选项卡重新定位?