在 MySQL 中使用正确的或更可取的不等于运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2066987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:59:57  来源:igfitidea点击:

Using the correct, or preferable, not equal operator in MySQL

mysqlsqlstandards-complianceinequality

提问by Rob Van Dam

Which of the two (semantically equivalent) ways is preferable to test for inequality?

两种(语义等价的)方法中哪一种更适合测试不平等?

  1. 'foo' != 'bar'(exclamation mark and equals sign)
  2. 'foo' <> 'bar'(less than and greater than chevron symbols together)
  1. 'foo' != 'bar'(感叹号和等号)
  2. 'foo' <> 'bar'(小于和大于 V 形符号加在一起)

The MySQL documentation clearly indicates that there is no difference between them and yet some people seem to be attached to only doing it one way or the other. Maybe this is just another pointless vi vs. emacs debate but when other people are reading your code (and therefore your queries), it's useful to maintain some consistency.

MySQL 文档清楚地表明它们之间没有区别,但有些人似乎只喜欢以一种或另一种方式来做。也许这只是另一个毫无意义的 vi 与 emacs 的争论,但是当其他人正在阅读您的代码(以及您的查询)时,保持一定的一致性很有用。

<>looks a lot like <=>which is a very underused operator but could perhaps lead to confusion at a quick glance since the two are nearly opposite (except for the obvious NULLcases).

<>看起来很像<=>which 是一个未被充分利用的运算符,但由于两者几乎相反(明显的NULL情况除外),因此快速浏览可能会导致混淆。

回答by jspcal

<> should be preferred, all things being equal, since it accords with the sql standard and is technically more portable...

<> 应该是首选,一切都相同,因为它符合 sql 标准并且在技术上更易于移植......

!= is non-standard, but most db's implement it.

!= 是非标准的,但大多数数据库都实现了它。

sql:2008 grammar:

sql:2008 语法:

<not equals operator> ::=
  <>

回答by S.Lott

It's obvious.

很明显。

The ! character is on the North West corner of US keyboards.

这 !字符位于美式键盘的西北角。

Microsoft headquarters are in the North West corner of the US.

微软总部位于美国的西北角。

So. <>is a nod to Microsoft.

所以。<>是对微软的致敬。

!=is a rejection of Microsoft.

!=是对微软的拒绝。

It's a secret political code.

这是一个秘密的密码。

回答by Joe

<> is the only one in the SQL-92 standard.

<> 是 SQL-92 标准中唯一的一个。

回答by JonH

DBA's generally like <> and programmers like !=. Just an observation :-)

DBA 通常喜欢 <>,而程序员喜欢 !=。只是一个观察:-)

回答by Adriaan Stander

They are the same, it is purely preference.

它们是相同的,这纯粹是偏好

This should give you a good idea

这应该给你一个好主意

Operators

运营商

!= (Not Equal To) Not equal to (not SQL-92 standard)

<> (Not Equal To) Not equal to

!=(不等于)不等于(非 SQL-92 标准)

<>(不等于)不等于

回答by Guffa

The <>operator is the one that is in the SQL standard, so most people that know SQL will be accustomed to it, or at least aware of it. I myself wasn't even aware of that the !=operator was also available in some SQL dialects until recently.

<>运营商是一个是在SQL标准,以便了解SQL大多数人会习惯它,或者至少意识到这一点。!=直到最近,我自己才意识到在某些 SQL 方言中也可以使用该运算符。

As you noticed, people tend to use only one or the other, and that is a good approach, at least on a project-by-project basis. Whichever you choose to use, be consistent.

正如您所注意到的,人们倾向于只使用一种或另一种,这是一种很好的方法,至少在逐个项目的基础上是这样。无论您选择使用哪种方式,都要保持一致。