在MySQL中使用正确的,或者更好的,不相等的运算符

时间:2022-12-17 11:48:05

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

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

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

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 NULL cases).

<>看起来很像<=>,这是一个非常不充分利用的运算符,但是由于两者几乎相反(除了明显的NULL情况),因此可能很快就会引起混淆。

6 个解决方案

#1


50  

<> 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.

!=非标准,但大多数db都实现它。

sql:2008 grammar:

sql:2008语法:

<not equals operator> ::=
  <>

#2


18  

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.

这是一个秘密的政治法典。

#3


4  

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

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

#4


2  

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

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

#5


2  

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)

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

<> (Not Equal To) Not equal to

<>(不等于)不等于

#6


0  

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.

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

#1


50  

<> 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.

!=非标准,但大多数db都实现它。

sql:2008 grammar:

sql:2008语法:

<not equals operator> ::=
  <>

#2


18  

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.

这是一个秘密的政治法典。

#3


4  

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

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

#4


2  

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

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

#5


2  

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)

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

<> (Not Equal To) Not equal to

<>(不等于)不等于

#6


0  

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.

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