如何创建sql查询以显示已读消息的计数器

vohkndzv  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(337)

我对sql非常陌生,希望有人能帮助我,我有一个名为project\u table(pjtid)的表和另一个名为snag\u table(外键sngproject\u id)的表,然后我有一个feedback\u table(外键fdb\u snag\u id)。我需要的是,当用户点击一个障碍,他们可以写一条消息,当他们返回到障碍列表时,它不能显示他们有任何消息等待他们,但用户的障碍被分配到登录,并看到有一个消息为他们。我如何处理它是否已被阅读是在其他地方处理。我构造了一个sql查询,其中显示了障碍列表和计数器。

SELECT  COUNT(CASE WHEN fdbRead = 2 THEN 1 ELSE NULL END) AS ReadCount,
        sngCreatedBy,sngAssignedToEmail,sngProject_ID,
        u1.useFullName as Createdname,
        sngCreatedBy,sngID,sngTitle,u2.useFullName as AssignedName,
        sngAssignedTo,sngStatusName,sngDescription,sngStartDate,
        sngDueDate,sngCreatedBy 
FROM Snag_Table 
    INNER JOIN User_Table u1 ON Snag_Table.sngCreatedBy = u1.useID 
    Left JOIN User_Table u2 ON Snag_Table.sngAssignedTo = u2.useID 
    LEFT JOIN FeedBack_Table ON Snag_Table.sngID = fdb_Snag_ID 
WHERE sngProject_ID = 10 
AND sngIsActive = 1 
GROUP BY sngCreatedBy,sngAssignedToEmail,sngProject_ID,
        u1.useFullName,Snag_Table.sngID,Snag_Table.sngTitle,
        u2.useFullName,sngAssignedTo,sngStatusName,sngDescription,
        sngStartDate,sngDueDate  
ORDER BY sngID DESC

我得到的是计数器,不管是谁登录的,谁创建的反馈。因此,我尝试在查询中添加一些逻辑,以根据登录者显示计数器,因此在where fdbcreatedby!=17

SELECT  COUNT(CASE WHEN fdbRead = 2 THEN 1 ELSE NULL END) AS ReadCount,
        sngCreatedBy,sngAssignedToEmail,sngProject_ID,
        u1.useFullName as Createdname,sngCreatedBy,
        sngID,sngTitle,u2.useFullName as AssignedName,
        sngAssignedTo,sngStatusName,sngDescription,sngStartDate,
        sngDueDate,sngCreatedBy 
FROM Snag_Table 
    INNER JOIN User_Table u1 ON Snag_Table.sngCreatedBy = u1.useID 
    Left JOIN User_Table u2 ON Snag_Table.sngAssignedTo = u2.useID 
    LEFT JOIN FeedBack_Table ON Snag_Table.sngID = fdb_Snag_ID 
WHERE *fdbCreatedBy != 17* 
AND sngProject_ID = 10 
AND sngIsActive = 1 
GROUP BY sngCreatedBy,sngAssignedToEmail,sngProject_ID,
        u1.useFullName,Snag_Table.sngID,Snag_Table.sngTitle,
        u2.useFullName,sngAssignedTo,sngStatusName,sngDescription,
        sngStartDate,sngDueDate  
ORDER BY sngID DESC

现在,计数器确实可以按预期工作,但前提是已经有了附加到障碍的反馈,但是如果我创建一个新的障碍并刷新列表,它不会显示障碍。任何帮助都将不胜感激。

9udxz4iz

9udxz4iz1#

这也许对你有帮助
从何处移除条件

WHERE *fdbCreatedBy != 17*

在你的陈述中加上这个条件

Count(CASE   WHEN (fdbread = 2 and  [fdbcreatedby] != 17 )THEN 1  ELSE NULL   END) AS readcount

相关问题