空表达式 [英] Null in case expression

查看:82
本文介绍了空表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在客户表中,许多客户在州列中没有州数据,因此当我们选择客户时,州列显示NULL值,这对报告目的没有意义。



 customerNumber customerName state country 
103 Atelier graphique NULL France
112 Signal Gift Stores NV USA





我试图通过使用案例来改善输出,但是收到错误:



你您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以便在'那'N / A'ELSE州END'附近使用正确的语法作为州,国家FROM客户'在第5行





我尝试过:



< pre> SELECT 
customerNumber,
customerName,
STATE 1 =(CASE State
When NULL Then'N / A'
ELSE state
END),
country
FROM
customers;

解决方案

MySQL CASE函数 [ ^ ]


除了理查德MacCutchan [ ^ ]的回答:

SELECT
customerNumber,
customerName,
CASE 状态 IS NULL < span class =code-keyword>然后 ' N / A' ELSE 状态 END AS 州,
国家/地区
FROM
customers;


In the customers' table, many customers do not have state data in the state column therefore when we select customers, the state column displays NULL values, which is not meaningful for the reporting purpose.

customerNumber   customerName         state   country
 103           Atelier graphique       NULL    France 
112           Signal Gift Stores        NV      USA



I am trying to improve the output by using the case, but getting an error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' THEN 'N/A' ELSE state END) as state, country FROM customers' at line 5"



What I have tried:

<pre>SELECT 
    customerNumber,
    customerName,
    STATE 1= ( CASE State
When NULL Then'N/A'
ELSE state
			 END),
    country
FROM
    customers;

解决方案

MySQL CASE Function[^]


In addition to Richard MacCutchan[^]'s answer:

SELECT 
    customerNumber,
    customerName,
    CASE When State IS NULL Then'N/A' ELSE State END AS State,
    country
FROM
    customers;


这篇关于空表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆