我是靠谱客的博主 闪闪白羊,最近开发中收集的这篇文章主要介绍【Mysql 学习】流程函数,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

--流程函数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博客 ” ,链接:http://blog.itpub.net/22664653/viewspace-683150/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/22664653/viewspace-683150/

最后

以上就是闪闪白羊为你收集整理的【Mysql 学习】流程函数的全部内容,希望文章能够帮你解决【Mysql 学习】流程函数所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部