是否有理由不使用< => (空安全等于运算符)在mysql中而不是=? [英] Is there a reason not to use <=> (null safe equals operator) in mysql instead of =?

查看:213
本文介绍了是否有理由不使用< => (空安全等于运算符)在mysql中而不是=?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MySQL提供了一个不错的运算符<=>,该运算符可与可能包含null的比较(例如null <=> nullnull <=> 5等)一起使用,并提供许多编程语言的直观结果.普通的equals运算符总是只返回null,这会引起很多新的MySQL用户(例如我本人)的不适.

MySQL provides a nice operator <=> that works with comparisons that could contain a null such as null <=> null or null <=> 5 etc. giving back intuitive results as many programming languages. Whereas the normal equals operator always just returns null, which catches many new MySQL users such as myself awry.

MySQL是否既有原因又有原因吗?<=>中是否有功能?谁真正需要使用内置语言类型实际上未定义的运算符?

Is there a reason MySQL has both and not JUST the functionality in <=> ? Who really needs an operator that is effectively undefined with built in language types?

推荐答案

谁真正需要使用内置语言类型实际上未定义的运算符?

Who really needs an operator that is effectively undefined with built in language types?

您要求提供一些实际示例.这是一个虚假的. 假设您有一个青少年住宿计划或类似计划,其中一项要求是孩子只能与同性的人同住一个房间.数据库中有一个可为空的M/F字段-可为空,因为您的数据馈送不完整(您仍在跟踪某些数据). 您的房间匹配代码绝对不应该匹配t1.Gender <=> t2.Gender的学生,因为它最终可能匹配两个性别未知的孩子,他们可能是异性.相反,您匹配它们相等的位置,而不是两者都为空.

You asked for some real-world examples. Here's a spurious one. Let's say that you have a residential youth programme or similar, and one of the requirements is that the kids only share a room with someone of the same sex. You have a nullable M/F field in your database - nullable because your data feed is incomplete (you're still chasing down some of the data). Your room-matching code should definitely not match students where t1.Gender<=>t2.Gender, because it could end up matching two kids of unknown gender, who might be of opposite genders. Instead, you match where they're equal and not both null.

那只是一个例子.我承认NULL=运算符的行为多年来引起了很多混乱,但最终的问题可能出在大量在线MySQL教程上,而没有提到NULL如何与运算符交互,也没有<=>运算符的存在.

That's just one example. I admit that the behaviour of NULL and the = operator have caused a lot of confusion over the years, but ultimately the fault probably lies with the plethora of online MySQL tutorials that make no mention of how NULL interacts with operators, nor of the existence of the <=> operator.

这篇关于是否有理由不使用&lt; =&gt; (空安全等于运算符)在mysql中而不是=?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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