我有三个整型变量:firstCount,secondCount,thirdCount。我需要比较它并输出结果。我不擅长SQL,但是C#代码是这样的:比较三个变量
if(firstCount == secondCount == thirdCount)
return true;
else
return false;
我有三个整型变量:firstCount,secondCount,thirdCount。我需要比较它并输出结果。我不擅长SQL,但是C#代码是这样的:比较三个变量
if(firstCount == secondCount == thirdCount)
return true;
else
return false;
下面是做这件事,但读@ 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
单向(2008语法)。
SELECT CAST(CASE
WHEN COUNT(DISTINCT C) = 1 THEN 1
ELSE 0
END AS BIT)
FROM (VALUES (@firstCount),
(@secondCount),
(@thirdCount)) t (C)
+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个值必须是相同的。
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
+1在我个人看来,这是在可读性和可维护性方面的最佳解决方案。 –
尝试代码,
if ((firstCount = secondCount) and (secondCount = thirdCount) and (firstCount = ThirdCount))
return true;
else
return false;
将不可能在C#中工作,并且不会在SQL工作。你需要'if(firstCount == secondCount && secondCount == thirdCount)...'。就目前来看,你正在做'((firstCount == secondCount)== thirdCount)',它的计算结果为'(布尔真/假)== thirdCount'。 – meagar
是否将零点视为平等或不平等? –
@Martin Smith不存在空值 – Wachburn