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

     

  • 相关阅读:
    超级钢琴 2010年NOI
    vijos P1375 大整数(高精不熟的一定要做!)
    COGS 445. [HAOI2010]最长公共子序列
    系统升级
    mariabd mysql升级mariadb
    mysql view 视图
    mysql 杂
    mysql主从复制
    DNS迭代查询与递归查询的区别
    Python 中 str 和 repr 的区别
  • 原文地址:https://www.cnblogs.com/perl6/p/7114742.html
Copyright © 2011-2022 走看看