zoukankan      html  css  js  c++  java
  • Mysql储存过程7: case

    #用在储存过程中:

    create
    procedure k() begin declare number int; set number = floor(rand(0)*2); case number when number > 0 then select '>0'; else select '=0'; end case; end$

    #用在查询中:
    select case user when 'root' then 'yes' else 'no' end from mysql.user;

    select case when user='root' then 'yes' else 'no' end from mysql.user;

    注意这个case用在储存过程中与用在查询语句中是不一样的。

    储存过程中要用end case结束, 用在一般查询中是end结束。

    #储存过程case语法
    case value
      when 条件 then
      SQL
      [when 条件 then]
      SQL
      [else]
      SQL
      end case;
    
    #用在搜索中的case语法:
    1.case when column=value then 'output1' else 'output2' end from database.table;
    2.case column when 'value' then 'output1' else 'output2' end from database.table;
    
    
    select case when column=value then 'output1' else 'output2' end  from database.table;
    
    mysql> select user,case  when user='root' then 'yes' else 'no' end from mysql.user;$
    +------+-------------------------------------------------+
    | user | case  when user='root' then 'yes' else 'no' end |
    +------+-------------------------------------------------+
    | root | yes                                             |
    | root | yes                                             |
    | root | yes                                             |
    +------+-------------------------------------------------+

     

    用法可参以参考一下这里:

    http://www.cnblogs.com/perl6/p/6995593.html

     

  • 相关阅读:
    表管理和索引,外键作用
    小场景 shell 应用
    高并发Linux系统及kernel参数优化
    H3C F100-M-G 配置2条数字专线
    yun update更新后 vm-workstation 找不到kernel
    布局页面
    第八节课
    第七堂课
    第六堂课 框架和样式表的基础
    第五节课 表单元素
  • 原文地址:https://www.cnblogs.com/perl6/p/7114742.html
Copyright © 2011-2022 走看看