如何将多行与来自两个不同 table 的其他行进行比较并检查它们是否相等?
How can I compare multiple rows with other rows from two different table and check if they are equal or not?
我有 2 个 table 名为 OptionText
和 SubmittedAns
第一个 table 包含 QuestionId
OptionId
和 IsRight
.这里 IsRight
用于表示问题的选项是否正确或 wrong.Here 一个 QuestionId
有多个 OptionId
甚至可以有一个。第二个table代表用户提交的内容。他们可以根据自己的假设 select 一个选项或多个选项。现在我需要制作自动脚本来证明提交的答案是对是错。
注意:如果一道题有多个正确选项,则用户必须select全部正确选项,如果缺少一个则结果为假。但是,如果他 select 的所有答案都是正确的,那么答案就是正确的。
我已经试过了script。它只能计算 selected 的数字,但不能证明答案是对还是错。所以我需要帮助。
我可以假设我需要一个 WHILE
循环来特别检查每个元素。但是怎么办?所以我需要帮助。这是我的代码。
CREATE TABLE OptionText(
[OptionTextId] [bigint] IDENTITY(1,1) NOT NULL,
[QuestionId] [bigint] NOT NULL,
[IsRightAnswer] [bit] NOT NULL)
Insert into OptionText (QuestionId, IsRightAnswer) VALUES (5, 1)
Insert into OptionText (QuestionId, IsRightAnswer) VALUES (5, 0)
Insert into OptionText (QuestionId, IsRightAnswer) VALUES (5, 0)
Insert into OptionText (QuestionId, IsRightAnswer) VALUES (17, 0)
Insert into OptionText (QuestionId, IsRightAnswer) VALUES (17, 1)
Insert into OptionText (QuestionId, IsRightAnswer) VALUES (17, 1)
CREATE TABLE SubmittedAns(
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[QuestionId] [bigint] NOT NULL,
[OptionTextId] [bigint] NOT NULL)
Insert into SubmittedAns (QuestionId, OptionTextId) VALUES (5, 1)
Insert into SubmittedAns (QuestionId, OptionTextId) VALUES (5, 2)
Insert into SubmittedAns (QuestionId, OptionTextId) VALUES (2, 1)
Insert into SubmittedAns (QuestionId, OptionTextId) VALUES (2, 1)
select * from OptionText
select * from SubmittedAns
if (select count(OptionTextId) from SubmittedAns where QuestionId =5) = (select count(ot.OptionTextId) from OptionText as ot where ot.IsRightAnswer = 1)
select 1 as "isRight"
else
select 0 as "isRight"
关键请参考第一行和最后一行material:
SELECT CASE COUNT(*) WHEN 0 THEN 'Pass' ELSE 'Fail' END AS Boolean
FROM (
SELECT *
FROM #OptionText
WHERE QuestionId = 5
AND IsRightAnswer = 1
) AS OT
FULL OUTER JOIN #SubmittedAns AS SA ON OT.QuestionId = SA.QuestionId AND OT.OptionTextId = SA.OptionTextId
WHERE SA.QuestionId = 5
AND OT.OptionTextId IS NULL -- This means some answers failed to be matched with your original questions/options, either because IsRightAnswer is zero, or because it doesn't exist in your questions/answers.
我在理解的路上得到了解决方案。所以我使用了一个函数来将它应用于所有问题并创建了这段代码。有效。
CREATE FUNCTION [dbo].[Set_IsCorrect_Answer_Deva]
(
@QuestionId BIGINT
)
RETURNS BIT
AS
BEGIN
DECLARE @IsRightAns BIT
DECLARE @count int
set @IsRightAns = 0
Declare @supplied_count int
select @supplied_count = Count(*) from SuppliedAnswersTemp where QuestionId=@QuestionId
IF(@supplied_count>0)
Begin
IF(@supplied_count=(select Count(*) from OptionText where QuestionId=@QuestionId and IsRightAnswer=1))
Begin
select @count=Count(*) from OptionText ot join SuppliedAnswersTemp sa on ot.QuestionId = sa.QuestionId
where ot.QuestionId= @QuestionId and ot.IsRightAnswer =1 and ot.Id=sa.OptionId
END
END
IF(@count>0)
Set @IsRightAns=1
RETURN @IsRightAns
END
我有 2 个 table 名为 OptionText
和 SubmittedAns
第一个 table 包含 QuestionId
OptionId
和 IsRight
.这里 IsRight
用于表示问题的选项是否正确或 wrong.Here 一个 QuestionId
有多个 OptionId
甚至可以有一个。第二个table代表用户提交的内容。他们可以根据自己的假设 select 一个选项或多个选项。现在我需要制作自动脚本来证明提交的答案是对是错。
注意:如果一道题有多个正确选项,则用户必须select全部正确选项,如果缺少一个则结果为假。但是,如果他 select 的所有答案都是正确的,那么答案就是正确的。
我已经试过了script。它只能计算 selected 的数字,但不能证明答案是对还是错。所以我需要帮助。
我可以假设我需要一个 WHILE
循环来特别检查每个元素。但是怎么办?所以我需要帮助。这是我的代码。
CREATE TABLE OptionText(
[OptionTextId] [bigint] IDENTITY(1,1) NOT NULL,
[QuestionId] [bigint] NOT NULL,
[IsRightAnswer] [bit] NOT NULL)
Insert into OptionText (QuestionId, IsRightAnswer) VALUES (5, 1)
Insert into OptionText (QuestionId, IsRightAnswer) VALUES (5, 0)
Insert into OptionText (QuestionId, IsRightAnswer) VALUES (5, 0)
Insert into OptionText (QuestionId, IsRightAnswer) VALUES (17, 0)
Insert into OptionText (QuestionId, IsRightAnswer) VALUES (17, 1)
Insert into OptionText (QuestionId, IsRightAnswer) VALUES (17, 1)
CREATE TABLE SubmittedAns(
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[QuestionId] [bigint] NOT NULL,
[OptionTextId] [bigint] NOT NULL)
Insert into SubmittedAns (QuestionId, OptionTextId) VALUES (5, 1)
Insert into SubmittedAns (QuestionId, OptionTextId) VALUES (5, 2)
Insert into SubmittedAns (QuestionId, OptionTextId) VALUES (2, 1)
Insert into SubmittedAns (QuestionId, OptionTextId) VALUES (2, 1)
select * from OptionText
select * from SubmittedAns
if (select count(OptionTextId) from SubmittedAns where QuestionId =5) = (select count(ot.OptionTextId) from OptionText as ot where ot.IsRightAnswer = 1)
select 1 as "isRight"
else
select 0 as "isRight"
关键请参考第一行和最后一行material:
SELECT CASE COUNT(*) WHEN 0 THEN 'Pass' ELSE 'Fail' END AS Boolean
FROM (
SELECT *
FROM #OptionText
WHERE QuestionId = 5
AND IsRightAnswer = 1
) AS OT
FULL OUTER JOIN #SubmittedAns AS SA ON OT.QuestionId = SA.QuestionId AND OT.OptionTextId = SA.OptionTextId
WHERE SA.QuestionId = 5
AND OT.OptionTextId IS NULL -- This means some answers failed to be matched with your original questions/options, either because IsRightAnswer is zero, or because it doesn't exist in your questions/answers.
我在理解的路上得到了解决方案。所以我使用了一个函数来将它应用于所有问题并创建了这段代码。有效。
CREATE FUNCTION [dbo].[Set_IsCorrect_Answer_Deva]
(
@QuestionId BIGINT
)
RETURNS BIT
AS
BEGIN
DECLARE @IsRightAns BIT
DECLARE @count int
set @IsRightAns = 0
Declare @supplied_count int
select @supplied_count = Count(*) from SuppliedAnswersTemp where QuestionId=@QuestionId
IF(@supplied_count>0)
Begin
IF(@supplied_count=(select Count(*) from OptionText where QuestionId=@QuestionId and IsRightAnswer=1))
Begin
select @count=Count(*) from OptionText ot join SuppliedAnswersTemp sa on ot.QuestionId = sa.QuestionId
where ot.QuestionId= @QuestionId and ot.IsRightAnswer =1 and ot.Id=sa.OptionId
END
END
IF(@count>0)
Set @IsRightAns=1
RETURN @IsRightAns
END