首页
技术知识库
Task工作计划
网站简介
DON框架
后台管理
文章分类
JAVA
框架知识
操作系统
容器相关
数据库层
优化技术
界面编程
网络编程
开发工具
GO语言
其他
读书随笔
观影随笔
每日随笔
APP
空值处理
所属分类
:[容器相关] |
创建时间
:2013-01-13 |
文章属性
:原创 |
文章来源
:http://thoughtfly.iteye.com |
作者
:windfly
<p> </p> <p style="margin: 0in; font-size: 10pt;"><span>COALESCE </span> <span> </span> </p> <p style="margin: 0in; font-size: 10pt;"><span> </span> <span>返回其参数中第一个非空表达式。</span> <span> </span> </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"><span> </span> <span>语法</span> <span> </span> </p> <p style="margin: 0in; font-size: 10pt;"> COALESCE ( expression [ ,...n ] ) </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"><span> </span> <span>参数</span> <span> </span> </p> <p style="margin: 0in; font-size: 10pt;"> expression </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"><span> </span> <span>任何类型的表达式。</span> <span> </span> </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"> n </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"><span> </span> <span>表示可以指定多个表达式的占位符。所有表达式必须是相同类型,或者可以隐性转换为相同的类型。</span> <span> </span> </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"><span> </span> <span>返回类型</span> <span> </span> </p> <p style="margin: 0in; font-size: 10pt;"><span> </span> <span>将相同的值作为</span> <span> expression </span> <span>返回。</span> <span> </span> </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"><span> </span> <span>注释</span> <span> </span> </p> <p style="margin: 0in; font-size: 10pt;"><span> </span> <span>如果所有自变量均为</span> <span> NULL</span> <span>,则</span> <span> COALESCE </span> <span>返回</span> <span> NULL </span> <span>值。</span> <span> </span> </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"><span> COALESCE(expression1,...n) </span> <span>与此</span> <span> CASE </span> <span>函数等价:</span> <span> </span> </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"> CASE </p> <p style="margin: 0in; font-size: 10pt;"> WHEN (expression1 IS NOT NULL) THEN expression1 </p> <p style="margin: 0in; font-size: 10pt;"> ... </p> <p style="margin: 0in; font-size: 10pt;"> WHEN (expressionN IS NOT NULL) THEN expressionN </p> <p style="margin: 0in; font-size: 10pt;"> ELSE NULL </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"><span> </span> <span>示例</span> <span> </span> </p> <p style="margin: 0in; font-size: 10pt;"><span> </span> <span>在下面的示例中,显示包含三列有关某个雇员每年工资收入信息的</span> <span> wages </span> <span>表:</span> <span>hourly_wage</span> <span>、</span> <span>salary </span> <span>和</span> <span> commission</span> <span>。但是,每个雇员只能接受一种付款方式。若要确定支付给所有雇员的工资总额,请使用</span> <span> COALESCE </span> <span>函数接受在</span> <span> hourly_wage</span> <span>、</span> <span>salary </span> <span>和</span> <span> commission </span> <span>中找到的非空值。</span> <span> </span> </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"> SET NOCOUNT ON </p> <p style="margin: 0in; font-size: 10pt;"> GO </p> <p style="margin: 0in; font-size: 10pt;"> USE master </p> <p style="margin: 0in; font-size: 10pt;"> IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES </p> <p style="margin: 0in; font-size: 10pt;"> WHERE TABLE_NAME = 'wages') </p> <p style="margin: 0in; font-size: 10pt;"> DROP TABLE wages </p> <p style="margin: 0in; font-size: 10pt;"> GO </p> <p style="margin: 0in; font-size: 10pt;"> CREATE TABLE wages </p> <p style="margin: 0in; font-size: 10pt;"> ( </p> <p style="margin: 0in; font-size: 10pt;"> emp_id tinyint identity, </p> <p style="margin: 0in; font-size: 10pt;"> hourly_wage decimal NULL, </p> <p style="margin: 0in; font-size: 10pt;"> salary decimal NULL, </p> <p style="margin: 0in; font-size: 10pt;"> commission decimal NULL, </p> <p style="margin: 0in; font-size: 10pt;"> num_sales tinyint NULL </p> <p style="margin: 0in; font-size: 10pt;"> ) </p> <p style="margin: 0in; font-size: 10pt;"> GO </p> <p style="margin: 0in; font-size: 10pt;"> INSERT wages VALUES(10.00, NULL, NULL, NULL) </p> <p style="margin: 0in; font-size: 10pt;"> INSERT wages VALUES(20.00, NULL, NULL, NULL) </p> <p style="margin: 0in; font-size: 10pt;"> INSERT wages VALUES(30.00, NULL, NULL, NULL) </p> <p style="margin: 0in; font-size: 10pt;"> INSERT wages VALUES(40.00, NULL, NULL, NULL) </p> <p style="margin: 0in; font-size: 10pt;"> INSERT wages VALUES(NULL, 10000.00, NULL, NULL) </p> <p style="margin: 0in; font-size: 10pt;"> INSERT wages VALUES(NULL, 20000.00, NULL, NULL) </p> <p style="margin: 0in; font-size: 10pt;"> INSERT wages VALUES(NULL, 30000.00, NULL, NULL) </p> <p style="margin: 0in; font-size: 10pt;"> INSERT wages VALUES(NULL, 40000.00, NULL, NULL) </p> <p style="margin: 0in; font-size: 10pt;"> INSERT wages VALUES(NULL, NULL, 15000, 3) </p> <p style="margin: 0in; font-size: 10pt;"> INSERT wages VALUES(NULL, NULL, 25000, 2) </p> <p style="margin: 0in; font-size: 10pt;"> INSERT wages VALUES(NULL, NULL, 20000, 6) </p> <p style="margin: 0in; font-size: 10pt;"> INSERT wages VALUES(NULL, NULL, 14000, 4) </p> <p style="margin: 0in; font-size: 10pt;"> GO </p> <p style="margin: 0in; font-size: 10pt;"> SET NOCOUNT OFF </p> <p style="margin: 0in; font-size: 10pt;"> GO </p> <p style="margin: 0in; font-size: 10pt;"> SELECT CAST(COALESCE(hourly_wage * 40 * 52, </p> <p style="margin: 0in; font-size: 10pt;"> salary, </p> <p style="margin: 0in; font-size: 10pt;"> commission * num_sales) AS money) AS 'Total Salary' </p> <p style="margin: 0in; font-size: 10pt;"> FROM wages </p> <p style="margin: 0in; font-size: 10pt;"> GO </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"><span> </span> <span>下面是结果集:</span> <span> </span> </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"> Total Salary </p> <p style="margin: 0in; font-size: 10pt;"> ------------ </p> <p style="margin: 0in; font-size: 10pt;"> 20800.0000 </p> <p style="margin: 0in; font-size: 10pt;"> 41600.0000 </p> <p style="margin: 0in; font-size: 10pt;"> 62400.0000 </p> <p style="margin: 0in; font-size: 10pt;"> 83200.0000 </p> <p style="margin: 0in; font-size: 10pt;"> 10000.0000 </p> <p style="margin: 0in; font-size: 10pt;"> 20000.0000 </p> <p style="margin: 0in; font-size: 10pt;"> 30000.0000 </p> <p style="margin: 0in; font-size: 10pt;"> 40000.0000 </p> <p style="margin: 0in; font-size: 10pt;"> 45000.0000 </p> <p style="margin: 0in; font-size: 10pt;"> 50000.0000 </p> <p style="margin: 0in; font-size: 10pt;"> 120000.0000 </p> <p style="margin: 0in; font-size: 10pt;"> 56000.0000 </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"> (12 row(s) affected) </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-weight: bold; font-size: 10pt;"><span>语法</span> <span> </span> </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in;"><span style="font-size: 10pt;"> </span> <span style="font-size: 12pt; color: #3366ff;">ISNULL ( check_expression , replacement_value )</span> <span style="font-size: 10pt;"> </span> </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"><span style="font-weight: bold;"> </span> <span style="font-weight: bold;">参数</span> <span> </span> </p> <p style="margin: 0in; font-size: 10pt;"> check_expression </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"><span> </span> <span>将被检查是否为</span> <span> NULL</span> <span>的表达式。</span> <span>check_expression </span> <span>可以是任何类型的。</span> <span> </span> </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"> replacement_value </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"><span> </span> <span>在</span> <span> check_expression </span> <span>为</span> <span> NULL</span> <span>时将返回的表达式。</span> <span>replacement_value </span> <span>必须与</span> <span> check_expresssion </span> <span>具有相同的类型。</span> <span> </span> </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"><span> </span> <span>返回类型</span> <span> </span> </p> <p style="margin: 0in; font-size: 10pt;"><span> </span> <span>返回与</span> <span> check_expression </span> <span>相同的类型。</span> <span> </span> </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"><span> </span> <span style="font-weight: bold;">注释</span> <span> </span> </p> <p style="margin: 0in; font-size: 10pt;"><span> </span> <span>如果</span> <span> check_expression </span> <span>不为</span> <span> NULL</span> <span>,那么返回该表达式的值;否则返回</span> <span> replacement_value</span> <span>。</span> <span> </span> </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"><span> </span> <span>示例</span> <span> </span> </p> <p style="margin: 0in; font-size: 10pt;"><span> A. </span> <span>将</span> <span> ISNULL </span> <span>与</span> <span> AVG </span> <span>一起使用</span> <span> </span> </p> <p style="margin: 0in; font-size: 10pt;"><span> </span> <span>下面的示例查找所有书的平均价格,用值</span> <span> $10.00 </span> <span>替换</span> <span> titles </span> <span>表的</span> <span> price </span> <span>列中的所有</span> <span> NULL </span> <span>条目。</span> <span> </span> </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"> USE pubs </p> <p style="margin: 0in; font-size: 10pt;"> GO </p> <p style="margin: 0in; font-size: 10pt;"> SELECT AVG(ISNULL(price, $10.00)) </p> <p style="margin: 0in; font-size: 10pt;"> FROM titles </p> <p style="margin: 0in; font-size: 10pt;"> GO </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"><span> </span> <span>下面是结果集:</span> <span> </span> </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"> -------------------------- </p> <p style="margin: 0in; font-size: 10pt;"> 14.24 </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"> (1 row(s) affected) </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"><span> B. </span> <span>使用</span> <span> ISNULL </span> </p> <p style="margin: 0in; font-size: 10pt;"><span> </span> <span>下面的示例为</span> <span> titles </span> <span>表中的所有书选择书名、类型及价格。如果一个书名的价格是</span> <span> NULL</span> <span>,那么在结果集中显示的价格为</span> <span> 0.00</span> <span>。</span> <span> </span> </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"> USE pubs </p> <p style="margin: 0in; font-size: 10pt;"> GO </p> <p style="margin: 0in; font-size: 10pt;"> SELECT SUBSTRING(title, 1, 15) AS Title, type AS Type, </p> <p style="margin: 0in; font-size: 10pt;"> ISNULL(price, 0.00) AS Price </p> <p style="margin: 0in; font-size: 10pt;"> FROM titles </p> <p style="margin: 0in; font-size: 10pt;"> GO </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"><span> </span> <span>下面是结果集:</span> <span> </span> </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"> Title Type Price </p> <p style="margin: 0in; font-size: 10pt;"> --------------- ------------ -------------------------- </p> <p style="margin: 0in; font-size: 10pt;"> The Busy Execut business 19.99 </p> <p style="margin: 0in; font-size: 10pt;"> Cooking with Co business 11.95 </p> <p style="margin: 0in; font-size: 10pt;"> You Can Combat business 2.99 </p> <p style="margin: 0in; font-size: 10pt;"> Straight Talk A business 19.99 </p> <p style="margin: 0in; font-size: 10pt;"> Silicon Valley mod_cook 19.99 </p> <p style="margin: 0in; font-size: 10pt;"> The Gourmet Mic mod_cook 2.99 </p> <p style="margin: 0in; font-size: 10pt;"> The Psychology UNDECIDED 0.00 </p> <p style="margin: 0in; font-size: 10pt;"> But Is It User popular_comp 22.95 </p> <p style="margin: 0in; font-size: 10pt;"> Secrets of Sili popular_comp 20.00 </p> <p style="margin: 0in; font-size: 10pt;"> Net Etiquette popular_comp 0.00 </p> <p style="margin: 0in; font-size: 10pt;"> Computer Phobic psychology 21.59 </p> <p style="margin: 0in; font-size: 10pt;"> Is Anger the En psychology 10.95 </p> <p style="margin: 0in; font-size: 10pt;"> Life Without Fe psychology 7.00 </p> <p style="margin: 0in; font-size: 10pt;"> Prolonged Data psychology 19.99 </p> <p style="margin: 0in; font-size: 10pt;"> Emotional Secur psychology 7.99 </p> <p style="margin: 0in; font-size: 10pt;"> Onions, Leeks, trad_cook 20.95 </p> <p style="margin: 0in; font-size: 10pt;"> Fifty Years in trad_cook 11.95 </p> <p style="margin: 0in; font-size: 10pt;"> Sushi, Anyone? trad_cook 14.99 </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"> (18 row(s) affected) </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p> </p> <p style="margin: 0in; font-size: 10pt;"><span style="font-weight: bold;">1.MSSQL: ISNULL()</span> </p> <p style="margin: 0in; font-size: 10pt;"><span style="font-weight: bold;">2.Oracle: NVL()</span> </p> <p style="margin: 0in; font-size: 10pt;"><span style="font-weight: bold;">3.Mysql: IFNULL()</span> </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;">在SQL Server Oracle MySQL 当数据库 中查出某值为NULL怎么办? </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"><span style="font-weight: bold;">1.MSSQL: ISNULL()</span> </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"><span style="font-weight: bold;">语法</span> </p> <p style="margin: 0in; font-size: 10pt;">ISNULL ( check_expression , replacement_value ) </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"><span style="font-weight: bold;">参数</span> </p> <p style="margin: 0in; font-size: 10pt;">check_expression </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;">将被检查是否为 NULL的表达式。check_expression 可以是任何类型的。 </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;">replacement_value </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;">在 check_expression 为 NULL时将返回的表达式。replacement_value 必须与 check_expresssion 具有相同的类型。 </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"><span style="font-weight: bold;">返回类型</span> </p> <p style="margin: 0in; font-size: 10pt;">返回与 check_expression 相同的类型。 </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"><span style="font-weight: bold;">注释</span> </p> <p style="margin: 0in; font-size: 10pt;">如果 check_expression 不为 NULL,那么返回该表达式的值;否则返回 replacement_value。 </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"><span style="font-weight: bold;">2.Oracle: NVL()</span> </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"><span style="font-weight: bold;">语法</span> </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;">NVL(eExpression1, eExpression2) </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"><span style="font-weight: bold;">参数</span> </p> <p style="margin: 0in; font-size: 10pt;">eExpression1, eExpression2 </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;">如果 eExpression1 的计算结果为 null 值,则 NVL() 返回 eExpression2。如果 eExpression1 的计算结果不是 null 值,则返回 eExpression1。eExpression1 和 eExpression2 可以是任意一种数据类型。如果 eExpression1 与 eExpression2 的结果皆为 null 值,则 NVL( ) 返回 NULL </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"><span style="font-weight: bold;">返回值类型</span> </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;">字符型、日期型、日期时间型、数值型、货币型、逻辑型或 null 值 </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"><span style="font-weight: bold;">说明</span> </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;">在不支持 null 值或 null 值无关紧要的情况下,可以使用 NVL( ) 来移去计算或操作中的 null 值。 </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"><span style="font-weight: bold;">3.Mysql: IFNULL()</span> </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"><span style="font-weight: bold;">语法</span> </p> <p style="margin: 0in; font-size: 10pt;">IFNULL(expr1,expr2) </p> <p style="margin: 0in; font-size: 10pt;"> </p> <p style="margin: 0in; font-size: 10pt;"><span style="font-weight: bold;">参数</span> </p> <p style="margin: 0in; font-size: 10pt;">expr1,expr2 </p> <p style="margin: 0in; font-size: 10pt;">假如expr1不是NULL,IFNULL()返回expr1,否则它返回expr2。IFNULL()返回一个数字或字符串值,取决于它被使用的上下文环境。</p> <p style="margin: 0in; font-size: 10pt;"> </p> <p> </p> <p style="margin: 0in; font-size: 10pt;"> </p>
返回