Sql Fiddle 一项任务可以重新分配给另一位开发人员。我想找到重新分配给特定开发人员的任务。
例如,任务 1 分配给用户 1,然后分配给用户 2,然后分配给用户 3。 如果用户 3 已登录,他可以在重新分配的任务列表中看到任务一。
以下是架构
CREATE TABLE IF NOT EXISTS `task` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`task_id` int(11) NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
查询
SELECT count(*) as num, a.user_id, max(a.id)
FROM task a
group by a.task_id
having num > 1
请帮我找到用户 3 之前与其他人以及现在与用户 3 的任务列表。
最佳答案
SELECT *
from task as t1
where user_id = 3 -- particular developer
and exists
(select *
from task as t2
where t2.task_id = t1.task_id -- same task
and t2.created < t1.created -- previously assigned
and t2.user_id <> t1.user_id -- to a different user
)
参见fiddle
关于php - Mysql-查询: How to find task assiged to which developer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33212647/