mysql流程函数if之类

表名: salary

————————

userid | salary|

————————

1  | 1000

2  | 2000

3  | 3000

4  | null

...

IF(value, t, f)    如果value是真, 返回t;否则返回f
        举例:
            select if(salary>2000, 'high', 'low') from salary
            注意: 这里会把null当成false处理
  
IFNULL(value1, value2)  如果value1不为空, 则返回value1, 否则返回value2
        举例:
            select ifnull(salary, 0) from salary

case when [value1] then [result1]... else [default] end   如果value1是真, 返回result,否则返回default
            类似if()但又不同于
        举例:
            select case when salary <= 2000 then 'low' else 'high' end from salary;
            注意: 这里会把null当成不满足条件处理, 得到high, 因为null 不是<= 2000的


case 【expr】 when 【value1】 then 【result】... else [default] end  如果expr等于value1, 返回result1, 否则返回default
            有点类似于python的if条件
        举例:
            select salary when 1000 then 'low' when 2000 then 'high' else 'mid' end


 

posted @ 2018-02-24 17:57  我当道士那儿些年  阅读(216)  评论(0编辑  收藏  举报