开发者

Help with MySQL Procedure Nested IF ELSE statments

开发者 https://www.devze.com 2023-04-05 12:22 出处:网络
Hi I need help with nested if else statements in MySQL. Please verify if the code below are the same? The C code is what I want to be accomplished in MySQL. I don\'t have syntax errors. But it seems t

Hi I need help with nested if else statements in MySQL. Please verify if the code below are the same? The C code is what I want to be accomplished in MySQL. I don't have syntax errors. But it seems that I am not getting the right result.

MySQL Stored Proc

IF top10_rank <= 10 AND top100_rank <=10 THEN SET temp_rank = 10;
ELSE SET temp_rank 开发者_如何转开发= 100;
END IF;


IF temp_rank = 10 THEN
  IF top10_rank_date > top100_rank_date THEN SET rank = top10_rank;
  ELSE SET rank = top100_rank;
  END IF;
ELSEIF temp_rank = 100 THEN SET rank = top100_rank;
ELSE SET rank = 0;
END IF;

C code

if(top10_rank <= 10 && top100_rank <=10)
{
    temp_rank = 10;
}
else
{
    temp_rank = 100;
}

if(temp_rank == 10)
{
    if(top10_rank_date > top100_rank_date)
    {
        rank = top10_rank
    }
    else
    {
        rank = top100_rank
    }
}
else if(temp_rank == 100)
{
    rank = top100_rank;
}
else
{
    rank = 0;
}


It seems that the pieces are equivalent without regarding such things as size of integer (? may be float) fields and handling of NULL values in SQL. Code looks not good:

1) This code is unreachable:

else
{
    rank = 0;
}

2) It could be shortened - temp_rank could be inlined

3) Probably you need use this function is SELECT, it could be rewritten with CASE operator - to make calls more effective

4) To detect a problem, wrap the C and SQL pieces in functions and specify for which input parameters results are different.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号