Mysql -query:如何查找分配给哪个开发人员的任务


Mysql -query : How to find task assiged to which developer

Sql Sql一个任务可以重新分配给另一个开发人员。我想找到重新分配给特定开发人员的任务。

例如,任务1分配给用户1,然后分配给用户2,然后分配给用户3。如果用户3已登录,他可以在重新分配的任务列表中看到任务1。

下面是模式

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
    )

看到小提琴