计算另一个表中一个用户表的行数

时间:2015-11-13 12:02:22

标签: mysql sql

我想为项目列表创建一个查询,该查询将提供已注册应用程序的数量,不包括用户不存在的应用程序。 在这种情况下,考虑到用户10不存在,我应该将查询结果作为以下内容:

结果

 +----+------------+--------------+
    | id |  project   | applications |
    +----+------------+--------------+
    |  1 | MyProject1 |            3 |
    |  2 | MyProject2 |            0 |
    |  3 | MyProject3 |            0 |
    +----+------------+--------------+

TABLES

   Projects
+----+------------+
| id |    name    |
+----+------------+
|  1 | MyProject1 |
|  2 | MyProject2 |
|  3 | MyProject3 |
+----+------------+


applications
+----+------+------------+
| id | user | project_id |
+----+------+------------+
|  1 |    3 |          1 |
|  2 |    4 |          1 |
|  3 |    5 |          1 |
|  4 |   10 |          1 |
+----+------+------------+



users
+----+---------+
| id |  Name   |
+----+---------+
|  1 | Smith   |
|  2 | John    |
|  3 | Paul    |
|  4 | Chris   |
|  5 | Gabriel |
+----+---------+

以下查询不排除不存在的用户:

SELECT  `projects` . * , (

                                                                                SELECT COUNT( * ) 
                                                                                FROM  `applications` 
                                                                                WHERE  `applications`.`project_id` =  `projects`.`id` 
                                                                                AND EXISTS (
                                                                                SELECT  `applications`.`id` 
                                                                                FROM  `applications` ,  `users`,`project`
                                                                                WHERE  `application`.`user` =  `users`.`id` AND `applications`.`project_id` = `project`.`id`
                                                                                )
                                                                                )  AS `applications`
                                                                                FROM  `projects` ORDER BY `id` DESC LIMIT 30

3 个答案:

答案 0 :(得分:1)

我认为您需要left joingroup by

select p.id, p.name, count(u.id)
from projects p left join
     applications a
     on p.id = a.project_id left join
     users u
     on a.user_id = u.id
group by p.id, p.name;

但是,您可能需要考虑修复数据。应用程序和项目以及应用程序和用户之间似乎应该存在外键关系。拥有无效用户的能力意味着与用户之间没有有效的外键关系。

答案 1 :(得分:0)

基于以前的解决方案

select p.id, p.name, count(u.id)
from projects p left join
     applications a
     on p.id = a.project_id left join
     users u
     on a.user = u.id
where u.id is not null
group by p.id, p.name;

执行左连接时,如果搜索值不存在,则返回null。然后通过排除空用户进行过滤,将为您提供结果。

请找一个sqlfiddle来说明它:http://www.sqlfiddle.com/#!9/cbfec6/3

但最简单的解决方案是

select p.id, p.name, count(u.id)
from projects p,applications a, users u
where a.user = u.id
 and p.id = a.project_id 
group by p.id, p.name;

答案 2 :(得分:0)

您的查询看起来过于复杂。这应该做:

select 
  id, 
  name as project, 
  (
    select count(*) 
    from applications a
    where a.project_id = p.id
    and a.user in (select id from users)
  ) as applications
from projects p;