SQL与可空值的等式/不等式比较 [英] SQL equality/inequality comparison with nullable values

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

问题描述

首先,kludge解决方案,sentinel方法(你的程序不能允许输入哨兵值是必须的):

 选择coalesce(a,-2147483648)= coalesce(b,-2147483648)as is_equal  -  a postgresism 

让我们说你忘记阻塞你的程序的哨兵值,用户在B字段上输入-2147483648,A为null。上面的代码报告true,应该报告false,不应该报告true或null。



什么是最简洁的方法来比较可空字段上的相等?

解决方案

A == B只应报告true或false,而不管字段是否为空。可能 IS [NOT] DISTINCT FROM 会在这里帮助。 / p>

first take, kludge solution, sentinel approach(it's imperative that your program should not allow inputting of sentinel value):

 select coalesce(a, -2147483648) = coalesce(b, -2147483648) as is_equal -- a little postgresism

let's say you forgot to block the sentinel value on your program, the user inputted -2147483648 on the B field, and A is null. the code above reports true, should report false, should not report true nor null.

what's the most concise way to compare equality on nullable fields? A == B should just report either true or false only, regardless if the field(s) are nullable or not.

解决方案

Probably IS [NOT] DISTINCT FROM will help here.

这篇关于SQL与可空值的等式/不等式比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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