我有一个名为employeetimesheets的表:
empsheet_id|employee_id|timesheet_status|last_update该表允许经理访问所有员工时间表。一个员工可以有几份工作时间表。我只想显示每个雇员最近的条目。我读过在手册中,我必须用inner join编写一个groupwise-maximum子查询和left join,但是我不知道怎么做。
到目前为止,这是我的查询::
$sqlempsheets="SELECT * FROM employeetimesheets JOIN employees ON employeetimesheets.employee_id=employees.employee_id WHERE employeetimesheets.timesheet_status='Pending Approval'";
$resultempsheets=mysqli_query($db,$sqlempsheets);发布于 2017-04-03 18:07:08
试试这个:
select *
from employeetimesheets t
join (
select employee_id,
max(empsheet_id) as empsheet_id
from employeetimesheets
group by employee_id
) t2 on t.employee_id = t2.employee_id
and t.empsheet_id = t2.empsheet_id
join employees e on t.employee_id = e.employee_id
where t.timesheet_status = 'Pending Approval';或者使用left join
select t.*, e.*
from employeetimesheets t
left join employeetimesheets t2 on t.employee_id = t2.employee_id
and t.empsheet_id < t2.empsheet_id
join employees e on t.employee_id = e.employee_id
where t.timesheet_status = 'Pending Approval'
and t2.employee_id is null;https://stackoverflow.com/questions/43191268
复制相似问题