`
13146489
  • 浏览: 246657 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Mysql函数:IFNULL,NULLIF,CASE

阅读更多
今天才用到了这几个函数
因为AVG(column)得出了null,又不想写在java代码就编码到sql中了。
下面是mysql官方文档
http://dev.mysql.com/doc/refman/5.1/en/control-flow-functions.html

Name Description
CASE Case operator
IF() If/else construct
IFNULL() Null if/else construct
NULLIF() Return NULL if expr1 = expr2
CASE value WHEN [compare_value] THEN result [WHEN [compare_value] THEN result ...] [ELSE result] END

CASE WHEN [condition] THEN result [WHEN [condition] THEN result ...] [ELSE result] END

The first version returns the result where value=compare_value. The second version returns the result for the first condition that is true. If there was no matching result value, the result after ELSE is returned, or NULL if there is no ELSE part.

mysql> SELECT CASE 1 WHEN 1 THEN 'one'
    ->     WHEN 2 THEN 'two' ELSE 'more' END;
        -> 'one'
mysql> SELECT CASE WHEN 1>0 THEN 'true' ELSE 'false' END;
        -> 'true'
mysql> SELECT CASE BINARY 'B'
    ->     WHEN 'a' THEN 1 WHEN 'b' THEN 2 END;
        -> NULL
The return type of a CASE expression is the compatible aggregated type of all return values, but also depends on the context in which it is used. If used in a string context, the result is returned as a string. If used in a numeric context, the result is returned as a decimal, real, or integer value.

Note
The syntax of the CASE expression shown here differs slightly from that of the SQL CASE statement described in Section 12.7.6.2, “CASE Statement”, for use inside stored programs. The CASE statement cannot have an ELSE NULL clause, and it is terminated with END CASE instead of END.

IF(expr1,expr2,expr3)

If expr1 is TRUE (expr1 <> 0 and expr1 <> NULL) then IF() returns expr2; otherwise it returns expr3. IF() returns a numeric or string value, depending on the context in which it is used.

mysql> SELECT IF(1>2,2,3);
        -> 3
mysql> SELECT IF(1<2,'yes','no');
        -> 'yes'
mysql> SELECT IF(STRCMP('test','test1'),'no','yes');
        -> 'no'
If only one of expr2 or expr3 is explicitly NULL, the result type of the IF() function is the type of the non-NULL expression.

The default return type of IF() (which may matter when it is stored into a temporary table) is calculated as follows.

Expression Return Value
expr2 or expr3 returns a string string
expr2 or expr3 returns a floating-point value floating-point
expr2 or expr3 returns an integer integer
If expr2 and expr3 are both strings, the result is case sensitive if either string is case sensitive.

Note
There is also an IF statement, which differs from the IF() function described here. See Section 12.7.6.1, “IF Statement”.

IFNULL(expr1,expr2)

If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2. IFNULL() returns a numeric or string value, depending on the context in which it is used.

mysql> SELECT IFNULL(1,0);
        -> 1
mysql> SELECT IFNULL(NULL,10);
        -> 10
mysql> SELECT IFNULL(1/0,10);
        -> 10
mysql> SELECT IFNULL(1/0,'yes');
        -> 'yes'
The default result value of IFNULL(expr1,expr2) is the more “general” of the two expressions, in the order STRING, REAL, or INTEGER. Consider the case of a table based on expressions or where MySQL must internally store a value returned by IFNULL() in a temporary table:

mysql> CREATE TABLE tmp SELECT IFNULL(1,'test') AS test;
mysql> DESCRIBE tmp;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| test  | varbinary(4) | NO   |     |         |       |
+-------+--------------+------+-----+---------+-------+
In this example, the type of the test column is VARBINARY(4).

NULLIF(expr1,expr2)

Returns NULL if expr1 = expr2 is true, otherwise returns expr1. This is the same as CASE WHEN expr1 = expr2 THEN NULL ELSE expr1 END.

mysql> SELECT NULLIF(1,1);
        -> NULL
mysql> SELECT NULLIF(1,2);
        -> 1
Note that MySQL evaluates expr1 twice if the arguments are not equal.
分享到:
评论

相关推荐

    浅谈Mysql中类似于nvl()函数的ifnull()函数

    如果expr1不是NULL,IFNULL()返回expr1,否则它返回expr2。IFNULL()返回一个数字或字符串值,取决于它被使用的上下文环境。 mysql&gt; select IFNULL(1,0); -&gt; 1 mysql&gt; select IFNULL(0,10); -&gt; 0 mysql&gt; select ...

    mysql常用函数汇总.chm

    Mysql的常用函数整体, 从网上收集的一些常用函数, ...NULLIF()函数将会检验提供的两个参数是否相等,如果相等,则返回NULL,如果不相等,就返回第一个参数。 如:SELECT NULLIF(1,1),NULLIF('A','B'),NULLIF(2+3,4+1);

    MySQL里的IFNULL、NULLIF和ISNULL用法

     mysql中isnull,ifnull,nullif的用法如下:  isnull(expr) 的用法:如expr 为null,那么isnull() 的返回值为 1,否则返回值为 0。mysql&gt; select isnull(1+1);-&gt; 0mysql&gt; select isnull(1/0);-&gt; 1使用= 的null ...

    MySql函数详解!!!

    MySQL数据库提供了很多函数包括: 数学函数:数学函数主要用于处理数字,包括整型、浮点数等。 字符串函数:字符串函数是MySQL中最常用的一类函数,字符串函数主要用于处理表中的字符串。 日期和时间函数:MySQL的...

    MySql中的IFNULL、NULLIF和ISNULL用法详解

    mysql中isnull,ifnull,nullif的用法如下: isnull(expr) 的用法: 如expr 为null,那么isnull() 的返回值为 1,否则返回值为 0。 mysql&gt; select isnull(1+1); -&gt; 0 mysql&gt; select isnull(1/0); -&gt; 1 使用= 的null ...

    PostgreSQL IF/IFNULL 函数 内核开发

    1. 博客:PostgreSQL的学习心得和知识总结(六十七)|语法级自上而下完美实现MySQL数据库的 IF函数和IFNULL函数 的实现方案

    mysql函数ifnull在pg 9.6中的实现

    工作中迁移mysql至pg 9.6,遇到mysql中的ifnull函数在pg中没有,pg中函数coalesce与ifnull功能相同,但函数名不同,需要修改应用。ifnull也在SQL标准中,pg此处不符合sql标准规范。本人尝试修改pg源码添加了ifnull...

    mysql中IFNULL,IF,CASE的区别介绍

    本文将详细介绍mysql中IFNULL,IF,CASE的区别,需要了解的朋友可以参考下

    mysql中null(IFNULL,COALESCE和NULLIF)相关知识点总结

    本文实例讲述了mysql中null(IFNULL,COALESCE和NULLIF)相关知识点。分享给大家供大家参考,具体如下: 在MySQL中,NULL值表示一个未知值,它不同于0或空字符串”,并且不等于它自身。 我们如果将NULL值与另一个...

    mysql函数大全,mysql

    STRCMP STRCMP()函数是MySQL里比较字符串的最简单方式之一。这个函数接受两个参数——要被比较的字符串。如果这个两个字符串相同,它就返回0;如果第一个大于第二个,它就返回1;如果第一个小于第二个,它就返回-1 ...

    浅谈SQLServer的ISNULL函数与Mysql的IFNULL函数用法详解

    SQL Serve的ISNULL函数: ISNULL(check_expression,replacement_value) 1、check_expression与replacement_value的数据类型必须一致。 2、如果check_expression为NULL,则返回replacement_value。 3、如果check_...

    浅谈Mysql中类似于nvl()函数的ifnull()函数.pdf

    浅谈Mysql中类似于nvl()函数的ifnull()函数.pdf

    mysql中替代null的IFNULL()与COALESCE()函数详解

    主要给大家介绍了关于mysql中替代null的IFNULL()与COALESCE()函数的相关资料,文中通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面来一起看看看吧。

    MySQL常用函数

    包含了MySQL常用的所有函数及详解。IFNULL(expr1,expr2),FROM_UNIXTIME。

Global site tag (gtag.js) - Google Analytics