我需要从两个查询减去结果有助于 [英] I need help with subtracting the results from two queries

查看:109
本文介绍了我需要从两个查询减去结果有助于的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    DECLARE @TotalQuestions int;
DECLARE @CorrectQuestions int;
DECLARE @IncorrectQuestions int;

SELECT (
  SET CorrectQuestion = SELECT COUNT( WiningComment)
    FROM Threads
    WHERE WiningComment IN (SELECT CommentsID
    FROM Comments
    WHERE  UsersID=@UserID)
) as 'WinningAnswers',
(
    SET TotalQuestions =  SELECT COUNT(CommentsID)
    FROM  Comments
    WHERE  UsersID=@UserID
) as 'TotalAnswers'
(
  SELECT  (TotalQuestions-CorrectQuestions ) //I am not sure about this part!!
) as 'IncorrectQuestions' 

我不知道最后一部分,我想从另一个子查询的结果减去一个子查询的结果

I am not sure about the last part, I want to subtract the results of one subquery from the results of another subquery

推荐答案

试试这个:

   DECLARE @TotalQuestions int;
DECLARE @CorrectQuestions int;
DECLARE @IncorrectQuestions int;

SELECT @CorrectQuestions = COUNT( WiningComment) 
    FROM Threads
    WHERE WiningComment IN (SELECT CommentsID
    FROM Comments
    WHERE  UsersID=@UserID)


    SELECT @TotalQuestions =  COUNT(CommentsID)
    FROM  Comments
    WHERE  UsersID=@UserID


  SELECT  @IncorrectQuestions = (@TotalQuestions-@CorrectQuestions ) 

Select @CorrectQuestions as 'WinningAnswers',
@TotalQuestions as 'TotalAnswers',
@IncorrectQuestions as 'IncorrectQuestions'

这篇关于我需要从两个查询减去结果有助于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆