• 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

     

  • 相关阅读:
    如何启动SOLR特性: 按层面检索
    solr的范围查询 TO
    jetty
    solr高亮的使用
    SQL日期加一天
    SQL从第二条开始取记录
    写出昨天的日期
    SQL取前后一条数据
    项目组【网站】的项目
    获取input文本框中高亮显示的值
  • 原文地址:https://www.cnblogs.com/perl6/p/7114742.html
Copyright © 2020-2023  润新知