MySQL 过程嵌套 IF ELSE 语句帮助
您好,我需要有关 MySQL 中嵌套 if else 语句的帮助。 请验证以下代码是否相同? C代码是我想在MySQL中完成的。我没有语法错误。但似乎我没有得到正确的结果。
MySQL存储过程
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代码
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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来这些片段是等效的,无需考虑诸如整数(?可能是浮点数)字段的大小以及 SQL 中 NULL 值的处理之类的事情。
代码看起来不太好:
1)此代码无法访问:
2)它可以缩短 - temp_rank 可以内联
3)可能您需要使用这个函数是 SELECT,它可以用 CASE 运算符重写 - 以使调用更有效
4)到检测问题,将 C 和 SQL 片段包装在函数中,并指定哪些输入参数结果不同。
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:
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.