-1

有列的表:number1,number2,number3

在程序中有查询:SELECT number1,number2,number3 FROM myTbl

,现在要检查

if number2 < number1 and number1 < number3 print 'OK'` 

怎么做?我不能为它写选择

4

6 回答 6

3

如果您需要选择:

SELECT number1, number2, number3
FROM YourTable
WHERE
  number2 < number1 
  AND number1 < number3

或者

SELECT 
  number1, number2, number3,
  CASE WHEN number2 < number1 AND number1 < number3 THEN 'OK' ELSE '' END AS Res
FROM YourTable    
于 2013-08-23T10:45:33.583 回答
3

问题有点不清楚,但如果你想有条件打印,试试这个

if exists (SELECT * FROM myTbl where number2 < number1 and number1 < number3)
    print 'Ok'
于 2013-08-23T10:47:13.653 回答
1

像这样的东西?

declare @test int;
select @test = case when number2<number1 and number1<number3 then 1 else 0 end
       from test1 where id=3;
if @test = 1 then   print 'OK'

SQL小提琴

于 2013-08-23T10:47:33.140 回答
0

我想你想要这个

if @number2 < @number1 and @number1 < @number3
Begin
    Print 'OK'
End

或这个

Select CASE WHEN ( @number2 < @number1 and @number1 < @number3THEN expression1) 
Then 'OK' Else '' End
于 2013-08-23T10:42:24.160 回答
0

我认为:

DECLARE @number1 int
DECLARE @number2 int
DECLARE @number3 int

SELECT 
    @number1 = number1,
    @number2 = number2,
    @number3 = number3
FROM myTbl
WHERE (some condition to get only row)

IF (@number2 < @number1 and @number1 < @number3)
BEGIN
    print 'OK'
END
于 2013-08-23T10:51:37.053 回答
0
declare @ID int = '' --im assuming you have some sort of ID and multiple rows in the table
declare @num1 int = (select number1 from mytable where [ID] = @ID)
declare @num2 int = (select number2 from mytable where [ID] = @ID)
declare @num3 int = (select number2 from mytable where [ID] = @ID)

IF @num2 < @num1 AND @num1 < @num3
begin
SELECT 'OK'
PRINT 'OK'
end

ELSE
begin
SELECT'OK'
PRINT 'OK'
END
于 2013-08-23T10:58:29.273 回答