我有一个员工表(ID)和表,当它改变状态(日期,状态,ID(FK))时。我需要显示根据指定日期的状态排序的员工列表
filteredRes.length
我试过任何日期$ My_Date,但我不知道如何修改所有更改,它只检查最后的日期更改
employees
ID, Name
1, Jack
2, Ralf
3, Jenny
changes
IDchange, Date, Status, ID
1, 2015-01-01, 2, 1 //Jack started in 2015-1-1 with status 2
2, 2015-03-01, 1, 2 //Ralf started in 2015-3-1 with status 1
3, 2015-04-01, 1, 3 //Jenny started in 2015-4-1 with status 1
4, 2015-08-01, 2, 2 //Ralf change status to 2 in 2015-8-1
5, 2015-10-01, 3, 2 //Ralf change status to 3 in 2015-10-1
6, 2016-04-01, 4, 1 //Jack change status to 4 in 2016-04-1
答案 0 :(得分:1)
您可以使用相关子查询来获得每位员工的status
ID
:
SELECT ID, Name, (SELECT Status
FROM changes AS c
WHERE e.ID = c.ID AND date <= ?
ORDER BY date DESC LIMIT 1) AS Status
FROM employees AS e
ORDER BY Status, Name ASC
子查询将返回当前员工的状态,该员工更接近参数化日期值。
答案 1 :(得分:0)
我们可以使用简单的连接查询来完成。当我们传递date
作为参数时,无需计算max(date)
,例如:
select e.name, s.date, s.status
from employee e join status s on e.id = s.id
where s.date = ?
order by s.status desc