简体   繁体   English

对CASE Mysql进行空检查

[英]Null checking on CASE Mysql

I have tried to check value, and if is it null(nothing to show) I deduce empty string. 我试图检查值,如果它为null(什么也没有显示),我推断出空字符串。 I was trying : 我正在尝试:

SELECT nullif(order_id, ' ') as order_id FROM label_pass_voucher WHERE order_id = 3447;
select CASE WHEN order_id then order_id else ' ' end as order_id FROM label_pass_voucher WHERE order_id = 3447;

it doesn't deduce nothing. 它不会演绎什么。 Where is my fault? 我的错在哪里 Thanks in advance 提前致谢

Use IFNULL(exp1, exp2) 使用IFNULL(exp1,exp2)

SELECT IFNULL(order_id, ' ') FROM label_pass_voucher WHERE order_id = 3447;

This syntax equivalent to exp1 == null ? exp2 : exp1 此语法等效于exp1 == null ? exp2 : exp1 exp1 == null ? exp2 : exp1

I assume that the query you're looking for is: 我假设您要查找的查询是:

SELECT IFNULL(order_id, '') AS order_id
FROM label_pass_voucher
WHERE order_id = 3447;

MySQL official documentation: MySQL官方文档:

If expr1 is not NULL, IFNULL() returns expr1; 如果expr1不为NULL,则IFNULL()返回expr1; otherwise it returns expr2. 否则返回expr2。

NULLIF() does a slightly different thing: NULLIF()做的事情略有不同:

Returns NULL if expr1 = expr2 is true, otherwise returns expr1. 如果expr1 = expr2为true,则返回NULL,否则返回expr1。 This is the same as CASE WHEN expr1 = expr2 THEN NULL ELSE expr1 END. 与expr1 = expr2 THEN NULL ELSE expr1 END时的情况相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM