在 OR 子句中使用 SQL 括号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5967890/
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 Parentheses use in an OR clause
提问by angela
Was wondering whether anyone would know why do we use the parentheses in this SQL: So, the format goes as follows:
想知道是否有人会知道我们为什么在此 SQL 中使用括号:因此,格式如下:
Name,location and department of the service of the employees whose name starts with A or B. (A rough translation from French).
姓名以 A 或 B 开头的员工的姓名、地点和服务部门。(法语的粗译)。
I answered the following way:
我是这样回答的:
SELECT service.nom_serv, localite.ville, localite.departemen
FROM service, localite, employe
WHERE service.code_loc=localite.code_loc
AND employe.service=service.code_serv
AND ((employe.nom LIKE 'A%') OR (employe.nom LIKE 'B%'))
Basically, where the last AND is concerned for the WHERE, couldn't I simply do without the parenthesis in order to have the SQL select for me employees with their name starting either with an A or a B? What difference does positioning a parenthesis in that way make? And ahy is there a double use of parentheses? Or is it to prioritize the OR in the last clause, since an AND is preceding it?
基本上,在最后一个 AND 与 WHERE 相关的地方,我不能简单地不用括号,以便为我的员工选择 SQL,他们的名字以 A 或 B 开头吗?以这种方式定位括号有什么区别?啊,括号有双重用途吗?或者它是否优先考虑最后一个子句中的 OR,因为 AND 在它之前?
回答by Damien_The_Unbeliever
Take a look at the Operator Precedencein SQL Server (You've not specified that, but I'd imagine it's the same for all RDBMS). What this means is that ANDs (without parenthesis) are evaluated before1bind more tightly than ORs.
看看SQL Server中的运算符优先级(您没有指定,但我想它对所有 RDBMS 都是一样的)。这意味着 AND(不带括号)在1绑定比 OR 更紧密之前进行评估。
So in your specific case, without the parenthesis, the conditions are:
所以在你的具体情况下,没有括号,条件是:
employe.service=service.code_serv AND employe.nom LIKE 'A%'
employe.service=service.code_serv AND employe.nom LIKE 'A%'
OR
或者
employe.nom LIKE 'B%'
employe.nom LIKE 'B%'
1Evaluation order is deliberately not specified in SQL, allowing many more possible re-orderings that languages that guarantee left-to-right or precedence ordered evaluation.
1SQL 中故意不指定求值顺序,允许更多可能的重新排序保证从左到右或优先顺序求值的语言。
回答by Dustin Laine
You use it to specify grouping of the clause, not priority. SQL does not allow you to specify priority as the optimizer will create the best priority for you.
您使用它来指定子句的分组,而不是优先级。SQL 不允许您指定优先级,因为优化器将为您创建最佳优先级。
AND ()
Will take both of the OR
conditions in one statement. So if either is true then the AND
is true as well. The inner parentheses are not necessary, but help in visualizing the separation.
将OR
在一个语句中同时满足这两个条件。因此,如果其中任何一个为真,那么它AND
也为真。内括号不是必需的,但有助于可视化分离。
Without the outer parentheses it would allow anything with the final clause as true as well.
如果没有外括号,它也允许最后一个子句为真。
回答by Dave
There are extra parenthesis. The rule in math is to add the parenthesis to clarify the logic. In this case if you remove all of the parenthesis you'll get the wrong answer. What you have is a AND ((b) OR (c)). Removing all of the parenthesis would take it from (a OR b) AND (a OR c) to (a AND b) OR c which is incorrect.
有额外的括号。数学中的规则是添加括号以阐明逻辑。在这种情况下,如果您删除所有括号,您将得到错误的答案。你所拥有的是一个 AND ((b) OR (c))。删除所有括号会将其从 (a OR b) AND (a OR c) 改为 (a AND b) OR c,这是不正确的。