2

我有两张桌子,一张有问题,一张有答案。问题架构由 questionid、question、module id 组成。一个moduleid 可能有很多问题。

Questionid   Question    Moduleid(FK)
--------     -----       -------
1            q1             1
2            q2             1
3            q4             2
4            q5             2

答案架构由 3 列组成

answerid     Answer    questionid(FK)
--------     -----       -------
1            a1             1
2            a2             2
3            a4             3
4            a5             4

我正在尝试构建一个存储过程,我将把 tblquestions 的 @moduleid 作为参数发送,如果特定 moduleid 的所有问题都在 tblquestions 中得到回答,我希望它返回“1”。(一个问题在 tblanswers 中只有一个答案)如果没有回答所有问题,则返回 0。关于如何制作这种方法的任何帮助?

4

3 回答 3

4

也许有一些更简单的东西,但这应该可以工作并且可以轻松修改它以返回所有已回答/未回答的模块或模块的缺失答案等。

SELECT CASE 
     WHEN EXISTS (SELECT 1 
                  FROM   tblquestions AS q 
                         INNER JOIN tblanswers AS a 
                                 ON q.questionid = a.questionid 
                  WHERE  ( q.moduleid = @ModuleId ) 
                         AND ( NOT EXISTS (SELECT 1 
                                           FROM   tblquestions AS q 
                                                  LEFT OUTER JOIN 
                                                  tblanswers AS a 
                                                               ON 
                                                  q.questionid = 
                                                  a.questionid 
                                           WHERE  ( q.moduleid = @ModuleId ) 
                                                  AND ( a.answerid IS NULL ) 
                                          ) )) 
   THEN 1  ELSE 0 
   END  AS AllQuestionsAnswered

这是带有所有模块ID的sql-fiddle,结果是:

  • 模块 ID = 1 => 1
  • 模块 ID = 2 => 1
  • 模块 ID = 3 => 0
于 2013-01-19T09:51:46.283 回答
2

将此查询放在存储过程中。

SELECT  CASE WHEN (COUNT(a.QuestionID) = COUNT(b.QuestionID))
            THEN 1 
            ELSE 0
        END as AreAllAnswered
FROM    questions a
        LEFT JOIN answers b
            ON a.QuestionID = b.questionID
WHERE   a.ModuleID = @valueHere

您看不到GROUP BY子句的原因是因为记录已经被特定过滤了ModuleID

完整的存储过程代码

CREATE PROCEDURE ProcNameHere (@ModuleID INTEGER) 
AS 
    SELECT  CASE WHEN (COUNT(a.QuestionID) = COUNT(b.QuestionID))
                THEN 1 
                ELSE 0
            END as AreAllAnswered
    FROM    questions a
            LEFT JOIN answers b
                ON a.QuestionID = b.questionID
    WHERE   a.ModuleID = @ModuleID
GO

更新 1

如果 OP 允许每个问题有多个答案,则查询只有轻微的修改,

CREATE PROCEDURE ProcNameHere (@ModuleID INTEGER) 
AS 
    SELECT  CASE WHEN (COUNT(DISTINCT a.QuestionID) = COUNT(DISTINCT b.QuestionID))
                THEN 1 
                ELSE 0
            END as AreAllAnswered
    FROM    questions a
            LEFT JOIN answers b
                ON a.QuestionID = b.questionID
    WHERE   a.ModuleID = @ModuleID
GO
于 2013-01-19T09:45:22.620 回答
0
 I hope this may work....

 declare @que int
 declare @ans int
 set @que=( select count(*) from Question where moduleid=1)
 set @ans=(select count(Answer.questionid) from Answer inner join Question
 on Question.Questionid=Answer.questionid where Question.moduleid=1)
 if(@que = @ans)
 begin
   select 1
 end
 else
 begin
   select 0
 end
于 2013-01-19T09:48:56.587 回答