php - Mysql-查询: How to find task assiged to which developer

标签 php mysql sql mysqli

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/

相关文章:

mysql - 如何使用 MySQL 主键作为变量

mysql - 在 MySQL/InnoDB 中锁定一行是否也总是锁定所有关联的索引记录?

sql - 嵌套表的最佳查询语法?

php - 如何使我的 PHP 元素更安全?

php - 如何显示用户在 MySQL 和 PHP 中出现一次的多个结果?

php - Opencart的类别 Controller 文件中的sort_order-ASC是什么?

sql - Oracle(旧?)加入 - 用于转换的工具/脚本?

php - 如何重新排序搜索结果?

php - 旋转图像降低质量

javascript - 如何水平增加此div的背景