Sql:查找答案是否存在

时间:2013-01-19 09:33:49

标签: sql sql-server sql-server-2008 stored-procedures

我有两个表,一个有问题,一个有答案。问题模式由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,如果在tblquestions中回答了特定moduleid的所有问题,我希望它返回“1”。 (一个问题在tblanswers中只有一个答案)或者如果不是所有问题都得到回答则返回0。有关如何采用这种方法的任何帮助吗?

3 个答案:

答案 0 :(得分: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

Here's the sql-fiddle包含所有模块ID,结果为:

  • ModuleID = 1 => 1
  • ModuleID = 2 => 1
  • ModuleID = 3 => 0

答案 1 :(得分: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

答案 2 :(得分: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