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

 

posted on 2017-07-04 08:02  Perl6  阅读(243)  评论(0编辑  收藏  举报

导航