简体   繁体   English

MySQL Left Join Count活动并返回null

[英]MySQL Left Join Count Activities and return null

Trying to get this mysql query to work. 试图让这个mysql查询工作。

Explanation: There are two tables, one with all projects with org id columns, the second has all activities with column projectid that relates to the project table. 说明:有两个表,一个表的所有项目的org id列,第二个表的所有活动的列projectid与项目表相关。 I would like to count all activities within the activities table that has the userid and is active with 1. But here comes the tricky part, I cannot get working. 我想计算活动表中具有用户ID并以1激活的所有活动。但是,这里出现了棘手的部分,我无法工作。 It does not collect the projects that are missing in activities table and present these with count 0. I would like to show all project that correspond with a unique projectid within bo 它不会收集活动表中缺少的项目,并以0计数。这些项目我想在bo中显示与唯一的projectid对应的所有项目。

SELECT p1.`id` AS projectid ,count(a1.`id`) AS activitiescount ,a1.`userid` AS userid ,p1.`orgid` AS orgid  
FROM `projects` AS p1 
LEFT JOIN `activities` AS a1 
ON p1.`id`=a1.`projectid` 
WHERE p1.`orgid`=3 
AND a1.`userid` = 26 
AND a1.`active`=1 
OR a1.`id` IS null 
GROUP BY p1.`id` 
ORDER BY p1.`name` DESC

Gives: 得到:

在此处输入图片说明

As you can see, the is not correct with 6 and 2 for example. 如您所见,例如6和2是不正确的。 Please help. 请帮忙。 Thank you. 谢谢。

Untested: 未经测试:

Moved limits on Activities to the join. 将活动限制添加到联接中。

Also the OR brought back more than you wanted, by moving the where clause around that problem should be resolved as well. 另外,通过解决该问题的where子句,OR也可以带回比您想要的更多的东西。

Standard Approach: 标准方法:

SELECT p1.`id` AS projectid 
     , count(a1.`id`) AS activitiescount 
     , a1.`userid` AS userid 
     , p1.`orgid` AS orgid  
FROM `projects` AS p1 
LEFT JOIN `activities` AS a1 
  ON p1.`id`=a1.`projectid` 
 AND a1.`userid` = 26 
 AND a1.`active`=1 
WHERE p1.`orgid`=3 
GROUP BY p1.`id` 
ORDER BY p1.`name` DESC

your approach w/ ()'s correctly placed. 您的方法(/)已正确放置。

SELECT 
    p1.`id` AS projectid 
    ,count(a1.`id`) AS activitiescount 
    ,a1.`userid` AS userid 
    ,p1.`orgid` AS orgid  

FROM `projects` AS p1 
LEFT JOIN `activities` AS a1 ON p1.`id`=a1.`projectid` 

WHERE p1.`orgid`=3 
  AND ((a1.`userid` = 26 AND a1.`active`=1)  OR a1.`id` IS null) 
GROUP BY p1.`id` 
ORDER BY p1.`name` DESC

----Update to simulate full outer join per comment. ----更新以模拟每个注释的完整外部联接。

SELECT * 
FROM (
SELECT p1.`id` AS projectid 
     , count(a1.`id`) AS activitiescount 
     , a1.`userid` AS userid 
     , p1.`orgid` AS orgid  
FROM `projects` AS p1 
LEFT JOIN `activities` AS a1 
  ON p1.`id`=a1.`projectid` 
 AND a1.`userid` = 26 
 AND a1.`active`=1 
WHERE p1.`orgid`=3 
GROUP BY p1.`id` 
UNION  
SELECT p1.`id` AS projectid 
     , count(a1.`id`) AS activitiescount 
     , a1.`userid` AS userid 
     , p1.`orgid` AS orgid  
FROM `projects` AS p1 
RIGHT JOIN `activities` AS a1 
  ON p1.`id`=a1.`projectid` 
 AND p1.`orgid`=3 
WHERE a1.`userid` = 26 
 AND a1.`active`=1 
GROUP BY p1.`id`)
ORDER BY p1.`id`,A1.ID -- assumes A1.ID exists and you want to order in that fashion.

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM