SQL嵌套计数查询

时间:2016-06-21 18:46:20

标签: mysql sql database

我有这些表格:

Discussion
----------
DiscussionID (Primary Key)
Name
....

UserDiscussion
--------------
UserID (Primary Key)
DiscussionID (Primary Key)
Participated (Boolean)
Bookmarked (Boolean)

我需要查询两件事:

  1. 某个用户参与的所有讨论
  2. 对于每次参加的讨论,我都需要为讨论添加书签的用户总数
  3. 这是我当前的查询:

    SELECT d.DiscussionID, d.Name
    FROM Discussion d LEFT JOIN
         UserDiscussion ud ON ud.DiscussionID = d.DiscussionID
    WHERE ud.UserID = 1 AND ud.Participated = true;
    

    示例数据:

    Discussion:
    
    DiscussionID | Name |
    ---------------------
    1              First
    
    UserDiscussion:
    
    UserID | DiscussionID | Participated | Bookmarked |
    ===================================================
        1          1              1              1
        4          1              1              0
    

    输出应如下所示:

    讨论:[   {     讨论ID:1,     名称:“第一”,     BookmarkCount:1   } ]

5 个答案:

答案 0 :(得分:0)

你可以使用带有subqyery的in子句,并计算

SELECT a.DiscussionID, a.Name, b.count(*)
FROM discussion a
INNER JOIN  UserDiscussion b 
WHERE a.DiscussionID in (
    SELECT d.DiscussionID
    FROM Discussion d
    INNER JOIN  UserDiscussion ud ON ud.DiscussionID = d.DiscussionID
    WHERE ud.UserID = 1 AND ud.Participated = true
)
AND b.Bookmarked  = true;
group by a.DiscussionID, a.Name

答案 1 :(得分:0)

SELECT
    d.DiscussionID
    ,d.Name
    ,Participated = SUM(CASE WHEN CAST(ISNULL(b.Participated,0) = 1 THEN 1 ELSE 0 END)
    ,Bookmarked = SUM(CASE WHEN CAST(ISNULL(b.Bookmarked,0) = 1 THEN 1 ELSE 0 END)
FROM
    Discussion d
    INNER JOIN UserDiscussion ud
    ON d.DiscussionID = ud.DiscussionID
    AND ud.Participated = true;
    LEFT JOIN UserDiscussion b
    ON d.DiscussionId = b.DiscussionId
GROUP BY
    d.DiscussionID
    ,d.Name

答案 2 :(得分:0)

听起来你有第一个查询,2d应该像

SELECT DiscussionID, count(UserID)
FROM UserDiscussion        
WHERE DiscussionID in (SELECT DiscussionID from UserDiscussion WHERE ud.UserID = 1 AND ud.Participated = true)
      AND Bookmarked = true
GROUP BY DiscussionID;

然后最终你需要加入他们

发布更正:

SELECT d.DiscussionID, d.Name, BookmarkCount
FROM Discussion d
JOIN UserDiscussion ud ON ud.DiscussionID = d.DiscussionID
JOIN (
SELECT DiscussionID as DID, count(UserID) as BookmarkCount
FROM UserDiscussion
WHERE DiscussionID in (SELECT DiscussionID from UserDiscussion WHERE
ud.UserID = 1 AND ud.Participated = true)
      AND Bookmarked = true
GROUP BY DiscussionID
) ON d.DiscussionID=DID;

答案 3 :(得分:0)

一种方法是将书签的次数写为

CREATE OR REPLACE VIEW v_times_bookmarked ( discussion_id, times_bookmarked ) AS SELECT discussion_id, count(*) FROM userdiscussion WHERE Bookmarked = true GROUP BY discussion_id

然后在您的主查询中,左连接到视图。

答案 4 :(得分:0)

您可以使用临时表来获取与每个讨论ID相关的书签计数

select DiscussionID, count(Bookmarked) as 'Bookmarked'
into #Bookmarks
from UserDiscussion
group by DiscussionID

然后,将它加入到DiscussionID上的最终表中,您可以将计数用作列。

SELECT d.DiscussionID, d.Name, b.Bookmarked
FROM Discussion d LEFT JOIN
     UserDiscussion ud ON ud.DiscussionID = d.DiscussionID
left join #Bookmarked b on ud.DiscussionID = b.DiscussionID
WHERE ud.UserID = 1 AND ud.Participated = true;