我对Sql很陌生,希望有人能帮忙,我有一个名为Project_Table(pjtID)的表和另一个名为Snag_Table(外键sngProject_ID)的表,然后我有一个FeedBack_Table(外键fdb_Snag_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
我得到的是计数器,而不管是谁登录以及谁创建了反馈。 因此,我尝试向查询中添加一些逻辑以根据登录的用户显示计数器,因此我在其中添加了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
现在,计数器可以按期望的方式工作,但前提是已经有附加到障碍物的反馈,但是如果我创建新障碍物,则刷新列表时不会显示障碍物。 任何帮助将不胜感激。
答案 0 :(得分:0)
这可能对您有帮助
从哪里删除条件
WHERE *fdbCreatedBy != 17*
并在您的案例声明中添加该条件
Count(CASE WHEN (fdbread = 2 and [fdbcreatedby] != 17 )THEN 1 ELSE NULL END) AS readcount