简体   繁体   English

如何将这两个查询合并为一个? (针对同一表的多个联接)

[英]How to combine these two queries into one? (multiple joins against the same table)

Given two tables, one for workers and one for tasks completed by workers, 给定两张桌子,一张给工人,一张给工人完成的任务,

CREATE TABLE IF NOT EXISTS `workers` (
  `id` int(11) NOT NULL,
  PRIMARY KEY  (`id`)
);

INSERT INTO `workers` (`id`) VALUES
(1);


CREATE TABLE IF NOT EXISTS `tasks` (
  `id` int(11) NOT NULL,
  `worker_id` int(11) NOT NULL,
  `status` int(11) NOT NULL,
  PRIMARY KEY  (`id`)
);

INSERT INTO `tasks` (`id`, `worker_id`, `status`) VALUES
(1, 1, 1),
(2, 1, 1),
(3, 1, 2),
(4, 1, 2),
(5, 1, 2);

I'm trying to get the number of tasks each worker has with each status code. 我正在尝试获取每个工作人员与每个状态代码对应的任务数。

I can say either 我可以说

SELECT w.*
,COUNT(t1.worker_id) as status_1_count
FROM workers w
LEFT JOIN tasks t1 ON w.id = t1.worker_id AND t1.status = 1 
WHERE 1 
GROUP BY 
t1.worker_id
ORDER BY w.id

or 要么

SELECT w.*
,COUNT(t2.worker_id) as status_2_count
FROM workers w
LEFT JOIN tasks t2 ON w.id = t2.worker_id AND t2.status = 2
WHERE 1 
GROUP BY 
t2.worker_id
ORDER BY w.id

and get the number of tasks with a single given status code, but when I try to get the counts for multiple task statuses in a single query, it doesn't work! 并使用单个给定的状态码获取任务数,但是当我尝试在单个查询中获取多个任务状态的计数时,它不起作用!

SELECT w.*
,COUNT(t1.worker_id) as status_1_count
,COUNT(t2.worker_id) as status_2_count
FROM workers w
LEFT JOIN tasks t1 ON w.id = t1.worker_id AND t1.status = 1 
LEFT JOIN tasks t2 ON w.id = t2.worker_id AND t2.status = 2
WHERE 1 
GROUP BY t1.worker_id
,t2.worker_id
ORDER BY w.id

The tasks table is cross-joining against itself when I would rather it wouldn't! 当我不想这样做时,tasks表将与自身交叉连接!

Is there any way to combine these two queries into one such that we can retrieve the counts for multiple task statuses in a single query? 有什么方法可以将这两个查询组合为一个,以便我们可以在单个查询中检索多个任务状态的计数?

Thanks! 谢谢!

SELECT w.*,
  SUM(t1.status = 1) AS status_1_count,
  SUM(t1.status = 2) AS status_2_count
FROM workers w
  LEFT JOIN tasks t1 ON w.id = t1.worker_id AND t1.status IN (1, 2) 
GROUP BY w.id
ORDER BY w.id;

I'm trying to get the number of tasks each worker has with each status code. 我正在尝试获取每个工作人员与每个状态代码对应的任务数。

SELECT worker_id, status, COUNT(*)
    FROM tasks
    GROUP BY worker_id, status;

That's all. 就这样。

I don't have an instance of MySQL here, but I tested this on a t-sql box and it worked. 我在这里没有MySQL实例,但是我在t-sql盒上对此进行了测试,并且可以正常工作。

select distinct(worker_id), 
       (select count(*) from tasks where status = 1) as Status1, 
       (select count(*) from tasks where status = 2) as Status2 
   from tasks;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将同一表上的多个MySQL查询合并为一个 - Combine Multiple MySQL Queries on the Same Table Into One 将两个查询合并到同一个表中的一个(相同列/不同行) - Combine two queries into one in same table (same columns/different rows) 将针对一个表的多个查询合并为一个结果集的多个字段 - Combine multiple queries against one table into multiple fields of one result set 在一个请求中将多个具有不同条件的SELECT查询合并到同一表中 - Combine Multiple SELECT queries with differents conditions in the same table in one request 如何将返回多列的两个 SQL 查询组合到同一个表的不同列中? - How can I combine two SQL queries returning multiple columns into separate columns of the same table? MySQL:如何将多个查询合并为一个使用具有不同条件的同一表的查询? - MySQL: How can I combine multiple queries into one that use same table with different conditionals? 如何将在同一表上执行的两个 SQL 查询合并为一个? - How to combine two SQL queries being performed on the same tables into one? 如何合并来自同一表的两个SQL查询 - How to combine two sql queries from same table 如何将这两个查询合并为一个 - how to combine these two queries into one 如何从一个表中合并两个不同查询的结果 - how to combine results of two different queries from one table mysql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM