mysql - Mysql/Laravel : Using left join to fetch latest created at date of any employee
问题描述
I'm trying to get all sections and their latest created_at
using left join
I'm using below query
select es.*, sul.created_at as created_at from section as es
left join section_update_logs as sul on es.id = sul.section_id
where sul.emp_id = 3
group by es.id
order by es.id, sul.created_at asc;
the obvious output of the query is only show that records where emp_id
is 3
, but i want all the records of section
table, if there is no employee record in section_update_logs
there should be a null/empty created_at.
Desired output is :
- all rows of table
section
created_at
date ifsul.section_id
=section.id
andemp_id
= of particular employee.
Laravel Query :
$result = DB::table("section as es")
->select('es.*', 'sul.created_at')
->leftJoin('section_update_logs as sul', 'es.id', '=', 'sul.section_id')
->orderBy('es.weight')
->orderBy('sul.created_at')
->orWhere('sul.emp_id', $emp_id)
->groupBy('es.id')
->get();
Please help
解决方案
You can add a closure to the leftJoin
function to add more conditions.
$result = DB::table("section as es")
->select('es.*', DB::raw('MAX(sul.created_at)'))
->leftJoin('section_update_logs as sul', function ($join) use ($emp_id) {
$join->on('es.id', '=', 'sul.section_id')
->where('sul.emp_id', $emp_id);
})
->orderBy('es.weight')
->orderBy('sul.created_at')
->groupBy('es.id')
->get();
This way, the filter on sul.emp_id
is only validated when a relation is found.
To get the same result with your sql query, add a second condition to your join using AND
:
select es.*, MAX(sul.created_at) as created_at from section as es
left join section_update_logs as sul on es.id = sul.section_id AND sul.emp_id = 3 -- HERE
group by es.id
order by es.id, sul.created_at asc;
推荐阅读
- python - 将 Dataloader 字典转换为 Pytorch 张量
- python - Python MultiProcessing:根据父进程中的事件杀死子进程
- twitter-bootstrap-3 - 媒体查询中的最大高度 Bootstrap 4
- python - 如何使此功能更有效?(板球统计)
- google-apps-script - 如何根据 Google 表单下拉值更新字段?
- python-3.x - 如何在请求中指定相当于 `--proxy-headers` 的 curl 参数?
- python - 如何在pyqt中从模型中设置组合框数据
- macos - 启动页、新标签页、默认搜索引擎 - Safari 扩展开发
- javascript - 使用 .val 函数列出不显示的字段内容
- javascript - 移入函数时,JQuery 扩展出现损坏