2011-11-09 63 views
2

我有三个整型变量:firstCountsecondCountthirdCount。我需要比较它并输出结果。我不擅长SQL,但是C#代码是这样的:比较三个变量

if(firstCount == secondCount == thirdCount) 
    return true; 
else 
    return false; 
+1

将不可能在C#中工作,并且不会在SQL工作。你需要'if(firstCount == secondCount && secondCount == thirdCount)...'。就目前来看,你正在做'((firstCount == secondCount)== thirdCount)',它的计算结果为'(布尔真/假)== thirdCount'。 – meagar

+1

是否将零点视为平等或不平等? –

+0

@Martin Smith不存在空值 – Wachburn

回答

2

下面是做这件事,但读@ meagar的评论之后,我发现这个解决方案更优雅。等待他把它变成答案...

DECLARE @firstcount INTEGER 
DECLARE @secondcount INTEGER 
DECLARE @thirdcount INTEGER 

SET @firstcount = 1 
SET @secondcount = 2 
SET @thirdcount = 3 

IF @firstcount <> @secondcount SELECT 0 
ELSE IF @secondcount <> @thirdcount SELECT 0 
ELSE SELECT 1 
+0

您忘记比较@ @ firstcount到@ @countcount。 – norlando

+1

@norlando - 我没有忘记,这是隐含的照顾。 –

+0

有没有办法用'select'构造来完成它?我必须将它返回给c#代码,但不能返回到消息窗口 – Wachburn

4

单向(2008语法)。

SELECT CAST(CASE 
     WHEN COUNT(DISTINCT C) = 1 THEN 1 
     ELSE 0 
     END AS BIT) 
FROM (VALUES (@firstCount), 
       (@secondCount), 
       (@thirdCount)) t (C) 
+0

+1了解了一些新的东西,谢谢。 –

1

你想说如果Firstcount等于秒计数,secondcount等于第三计数返回true。

你可以做

DECLARE @a INT = 2 
DECLARE @b INT = 2 
DECLARE @c INT = 2 

IF (@a = @b AND @a = @c) 
BEGIN 
    Print('true') 
END 
ELSE 
BEGIN 
    Print('false') 
END 

虽然它没有什么你不差需要测试B = C因为A = C是完全一样的东西,因为所有3个值必须是相同的。

3
declare @first int 
     , @second int 
     , @third int 

     select @first = 0 
      , @second = 0 
      , @third = 0 

select case when (@first = @second) AND (@second = @third) THEN 1 ELSE 0 END 
+0

+1在我个人看来,这是在可读性和可维护性方面的最佳解决方案。 –

0

尝试代码,

if ((firstCount = secondCount) and (secondCount = thirdCount) and (firstCount = ThirdCount)) 
    return true; 
else 
    return false;