ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 【Mysql 学习】流程函数

【Mysql 学习】流程函数

原创 Linux操作系统 作者:杨奇龙 时间:2011-01-01 22:33:38 0 删除 编辑

--流程函数。if ,ifnull ,case 语句!
mysql> create table sal (id int,sal decimal (9,2));
Query OK, 0 rows affected (0.06 sec)
mysql> insert into sal values (1,1000),(2,2000),(3,3000),(4,4000),(5,null);
Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0
--if(EXP,T,F) EXP 为真则返回T,EXP为假,则返回F
mysql> select * from sal;
+------+---------+
| id   | sal     |
+------+---------+
|    1 | 1000.00 |
|    2 | 2000.00 |
|    3 | 3000.00 |
|    4 | 4000.00 |
|    5 |    NULL |
+------+---------+
5 rows in set (0.00 sec)

mysql> select id, if(sal > 3000,'high','low') from sal;
+------+-----------------------------+
| id   | if(sal > 3000,'high','low') |
+------+-----------------------------+
|    1 | low                         |
|    2 | low                         |
|    3 | low                         |
|    4 | high                        |
|    5 | low                         |--这里null >3000 为假!
+------+-----------------------------+
5 rows in set (0.01 sec)
--IFNULL(VAL,N)如果val 为null 则返回N
mysql> select id, ifnull(sal,0) from sal;
+------+---------------+
| id   | ifnull(sal,0) |
+------+---------------+
|    1 |       1000.00 |
|    2 |       2000.00 |
|    3 |       3000.00 |
|    4 |       4000.00 |
|    5 |          0.00 |
+------+---------------+
5 rows in set (0.01 sec)
--case 语句和oracle的一样了!
mysql> select case when sal<=3000 then 'low' else 'high' end from sal;
+------------------------------------------------+
| case when sal<=3000 then 'low' else 'high' end |
+------------------------------------------------+
| low                                            |
| low                                            |
| low                                            |
| high                                           |
| high                                           |
+------------------------------------------------+
5 rows in set (0.00 sec)
mysql> select id, case sal when 2000 then 'low' when 3000 then 'mid' else 'high' end from sal;
+------+--------------------------------------------------------------------+
| id   | case sal when 2000 then 'low' when 3000 then 'mid' else 'high' end |
+------+--------------------------------------------------------------------+
|    1 | high                                                               |
|    2 | low                                                                |
|    3 | mid                                                                |
|    4 | high                                                               |
|    5 | high                                                               |
+------+--------------------------------------------------------------------+
5 rows in set (0.01 sec)

这里只是简单介绍,跟多的需要在实践中学习!抛砖引玉了,呵呵

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/22664653/viewspace-683150/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
MySQL DBA NoSQL DEVOPS

注册时间:2009-10-07

  • 博文量
    1026
  • 访问量
    8206008