简体   繁体   中英

SQL uses of "less than or equal to" <= vs. "not greater than" !> operators

Why are there two different comparison operators that seem to do the same thing.

Is there any situation where one would be prefered over the other?

<= and > are comparison operators, not logical operators. ! is a logical operator (means NOT). When you combine ! and >, you're simply inverting a comparison operator, so your end result is the same.

Having said that, <= is the common form, so I'd say it's preferred, for readability if nothing else. I don't know if there's a performance benefit to either, but I doubt it.

Edit: Also, you didn't say which flavor of SQL you're dealing with. As @harryovers pointed out, that's a valid operator in MS-SQL, but it might not work everywhere.

我不明白您为什么要使用其中一个,但是!>不在 ISO 标准中,基于此,我会说<=是首选方式。

One reason to have the !> alternaive is to make it easy to put SQL inside XML. The less than sign introduces XML tags. If SQL with a < is included in either XML or HTML it would have to be escaped as &LT;. A greater than sign doesn't mean anything special unless it's been preceded by a less than sign.

The != , !< and !> are not standard comparison operators and are only supported by few systems, SQL-Server being one: msdn: Comparison Operators (Transact-SQL) . MySQL also supports != but only that, not the other two.

The equivalent standard SQL comparison operators are <> , >= and <= .

In all situations, I would prefer the standard. You don't know when you have to migrate your code to another platform (and have less errors to deal with.)

No, there's no difference. Only reason I can think of is to make it more human-readable in a certain context.

Eg for the same reason I'd use < 5 rather than <= 4 if there was a significance to the 5 representing some limit in the context.

When you say logical operators logical is AND and OR I've never seen !> I've seen <>

If you are referring to != and <> they are the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM