SQL查询中条件表达式的顺序? [英] The order of condition expressions in SQL queries?

查看:101
本文介绍了SQL查询中条件表达式的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道至少在C和C#中条件的顺序很重要.例如,以下两个条件是不同的:
1.(str1.Length> 0 && str1 [0] ==``A'')
这很好,是正确的.
2.(str1 [0] ==''A''&& str1.Length> 0)
这太荒谬了,因为执行命令的顺序(在这种情况下)是从左到右,如果str1.Length = 0,则将引发异常,因此它将无法正常工作.这只是一个简单的例子.但是我想知道的是,在WHERE,HAVING,ON或其他可能的条件之后,SQL查询在条件下的工作方式是否相同?
非常感谢!

I know the order of conditions is important in at least C and C#. For example, the two following conditions are different:
1. (str1.Length > 0&&str1[0] == ''A'')
This works well and is the correct one.
2. (str1[0] == ''A''&&str1.Length>0)
This is too ridiculous because the order of executing commands (in this case) is from left to right, if str1.Length = 0, an exception will be thrown, so it can''t work. This is only a simple example. But what I want to know is Do SQL queries work the same way with conditions after WHERE, HAVING, ON or anywhere else possible?
Thank you so much!

推荐答案

您是否想要坏消息?抱歉,SQL的行为与C,C ++和C#不同.在这种情况下,AND子句始终从左至右执行,任何错误结果都会中止该操作(称为评估短路"). SQL不会那样做.它的作用更为复杂.此处有描述: SQL Server如何短路WHERE条件评估 [
Do you want the bad news? Sorry, but SQL does not behave the same as C, C++ and C#. In those, AND clauses are always executed left - to right, and any false result will abort the operation (called "evaluation short-circuiting"). SQL does not do that. What it does do is more complex. There is a description here: How SQL Server short circuits WHERE condition evaluation[^]

Gets my five for a very good question, though!


这篇关于SQL查询中条件表达式的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆