sql 使用“小于或等于”和“不大于”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7999435/
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
sql uses of "less than or equal to" & "not greater than"
提问by harryovers
Why are there two different logical operators that seem to do the same thing (<= & !>), is there any situation where one would be prefered over the other?
为什么有两个不同的逻辑运算符似乎在做同样的事情(<= & !>),是否有任何一种情况比另一个更受欢迎?
采纳答案by ddrace
<= 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.
<= 和 > 是比较运算符,而不是逻辑运算符。!是逻辑运算符(表示 NOT)。当你结合!和 >,您只是反转比较运算符,因此您的最终结果是相同的。
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.
编辑:另外,您没有说明您正在处理哪种 SQL。正如@harryovers 指出的那样,这是 MS-SQL 中的有效运算符,但它可能不适用于任何地方。
回答by James Johnson
I can't see why you would use one over the other, but !>
is not in ISO standards, and based on that I would say that <=
is the preferred way.
我不明白为什么你会使用一个而!>
不是另一个,但不在 ISO 标准中,基于此我会说这<=
是首选方式。
回答by Michael Resnick
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 <. A greater than sign doesn't mean anything special unless it's been preceded by a less than sign.
使用 !> 替代选项的原因之一是可以轻松地将 SQL 放入 XML 中。小于号引入了 XML 标签。如果带有 < 的 SQL 包含在 XML 或 HTML 中,则必须将其转义为 <。一个大于号并不意味着任何特殊的东西,除非它前面有一个小于号。
回答by ypercube??
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.
的!=
,!<
而!>
不是标准的比较操作,并且只有少数系统支持,SQL-服务器是一个:MSDN:比较运算符(Transact-SQL) 。MySQL 也支持!=
但仅此而已,不支持其他两个。
The equivalent standard SQL comparison operators are <>
, >=
and <=
.
等效的标准 SQL 比较运算符是<>
,>=
和<=
。
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.)
在所有情况下,我更喜欢标准。您不知道何时必须将代码迁移到另一个平台(并且要处理的错误更少。)
回答by Widor
No, there's no difference. Only reason I can think of is to make it more human-readable in a certain context.
不,没有区别。我能想到的唯一原因是使其在特定上下文中更具人类可读性。
E.g. for the same reason I'd use < 5
rather than <= 4
if there was a significance to the 5representing some limit in the context.
例如,出于同样的原因,我会使用< 5
而不是<= 4
如果5代表上下文中的某些限制有重要意义。
回答by JonH
When you say logical operators logical is AND
and OR
I've never seen !> I've seen <>
当你说逻辑运算符逻辑是AND
,OR
我从未见过!> 我见过<>
If you are referring to !=
and <>
they are the same.
如果您指的是它们!=
,<>
它们是相同的。