if在数据库mysql存储中判断

428次阅读
没有评论

if在数据库mysql存储中判断

1.语法:

IF 条件判断 THEN 结果
    [ELSEIF 条件判断 THEN 结果] ...
    [ELSE 结果]
END IF

举例:传入所查询的id参数查询工资标准(s<=6000为低工资标准;6000 <=10000为中工资标准;10000 <=15000为中上工资标准;s style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">=15000为高工资标准)

delimiter //
create procedure s_sql(in val_id int)
begin
    # 声明一个局部变量result存放工资标准结果
    declare result varchar(32);
    # 声明一个局部变量存放查询得到的工资
    declare s double;
    # 根据入参id查询工资
    select salary into s from employee where id = val_id;
    # if判断的使用
    if s <= 6000 then
        set result = '低工资标准';
    elseif s <= 10000 then
        set result = '中工资标准';
    elseif s <= 15000 then
       set result = '中上工资标准';
    else
        set result = '高工资标准';
    end if;
    # 查询工资标准结果
    select result;
end //
 
# 调用函数,传入参数
call s_sql(1);

2.IF函数也能通过判断条件来返回特定值

它的语法如下

IF(expr,result_true,result_false)

expr是一个条件表达式,如果结果为true,则返回result_true,否则返回result_false。

if函数不仅可以调用参数进行条件的筛选,同样还有上访的返回特定值的功能,具体的用法小伙伴们还需要亲自到实例中使用代码才能体验,感兴趣的快动手看看成果。

本文教程操作环境:windows7系统、mysql5.8,DELL G3电脑。

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:Python教程2022-10-19发表,共计1021字。
新手QQ群:570568346,欢迎进群讨论 Python51学习