即使没有返回数据,也可以检索COUNT()

时间:2012-01-27 15:30:03

标签: mysql sql count

我试图用一个查询从数据库中检索一些东西: - 朋友请求的数量 - 新私人消息的数量 - 通知数量 - 和用户的用户名,例如。将PM发送给您或邀请您加入朋友。

查询中下面指定的用户ID是检查该用户是否有任何通知的用户。

问题是:如果没有通知,它也不会返回朋友请求或私人消息的数量。

这是我构建的查询,它与上述问题不同:

SELECT 
    n.id, 
    n.type,
    u.username,
    COUNT(fr.friend_id) as frc,
    COUNT(pm.pm_id) as pmc
FROM 
    notifications as n,
    users as u
LEFT JOIN
    private_messages as pm
ON
    pm.to_user = '1'
AND 
    pm.status = 'unread'
LEFT JOIN
    friends as fr
ON  
    fr.friend_id = '1'
AND
    fr.status = 'pending'
WHERE 
    n.user_id='1'
AND
    u.id = n.from_id
ORDER BY
    n.id ASC

我不确定我缺少什么,所以如果有人可以分享他们的sql知识并帮助我,我会非常感激。

谢谢!

聚苯乙烯。我对更复杂的查询仍然很新,所以如果我完全偏离上述查询,请告诉我:)。

表结构:

Sample friends table structure
+--------------------------+-----------------------------+
| Field                    | Type                        |
+--------------------------+-----------------------------+
| user_id                  | int(10)                     |
| friend_id                | int(10)                     |
| status                   | ENUM('pending','accepted')  |
| user_id                  | int(10)                     |
+--------------------------+-----------------------------+

Sample notifications table structure
+--------------------------+---------------------+
| Field                    | Type                |
+--------------------------+---------------------+
| id                       | int(10)             |
| type                     | ENUM('pm','friend') |
| user_id                  | int(10)             |
| from_id                  | int(10)             |
+--------------------------+---------------------+

Sample private message table structure
+--------------------------+-----------------------+
| Field                    | Type                  |
+--------------------------+-----------------------+
| pm_id                    | int(12)               |
| to_user                  | int(10)               |
| from_user                | int(10)               |
| status                   | ENUM('read','unread') |
+--------------------------+-----------------------+

Sample users table structure
+--------------------------+---------------+
| Field                    | Type          |
+--------------------------+---------------+
| id                       | int(10)       |
| username                 | varchar(50)   |
+--------------------------+---------------+

3 个答案:

答案 0 :(得分:1)

抱歉,我不知道mysql语法,但你可以尝试

...
From 
   users as u
LEFT JOIN 
   notifications as n ON <whatever>
...

你所拥有的东西可能与我所知道的完全相同。但假设您的用户表始终有数据,这可能会有效。

答案 1 :(得分:0)

或许被遗忘的小组:

WHERE 
        n.user_id='1'
    AND
        u.id = n.from_id
   GROUP BY  n.id, 
    n.type,
    u.username
    ORDER BY
        n.id ASC

干杯

答案 2 :(得分:0)

您的查询会混合不相关的内容 - 通知的列表标量计数集。它还会影响您是否获得至少有一个通知要求的任何结果。从设计的角度来看,这两者不应该是混合的,所以我会把它重新写成两个查询。

您应该使用correlated subquery来计算:

select
    u.username,
    (select COUNT(fr.friend_id) from friends as fr where fr.friend_id = u.id AND fr.status = 'pending') as frc,
    (select COUNT(pm.pm_id) from private_messages as pm where pm.to_user = u.id AND pm.status = 'unread') as pmc
from users u
where u.id='1'

通知查询是原始查询的简化版本:

SELECT 
    n.id, 
    n.type,
    u.username
FROM 
    users as u
LEFT JOIN
    notifications as n ON n.from_id=u.id
WHERE
    u.id = '1'
ORDER BY
    n.id ASC