Is there a reason not to use <=> (null safe equals operator) in mysql instead of =?

后端 未结 5 2269
自闭症患者
自闭症患者 2021-02-13 16:47

MySQL provides a nice operator <=> that works with comparisons that could contain a null such as null <=> null or null <=> 5

5条回答
  •  青春惊慌失措
    2021-02-13 17:04

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

    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.

    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.

提交回复
热议问题