SQL OR 子句中使用括号
想知道是否有人知道为什么我们在这个 SQL 中使用括号: 因此,格式如下:
姓名以 A 或 B 开头的员工的姓名、地点和服务部门。(法语的粗略翻译)。
我的回答如下:
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%'))
基本上,最后一个 AND 涉及 WHERE,难道我不能简单地不使用括号,以便让 SQL 为我选择姓名以 A 或 B 开头的员工吗?以这种方式放置括号有什么区别?啊,括号有双重使用吗?或者是优先考虑最后一个子句中的 OR,因为 AND 位于它之前?
Was wondering whether anyone would know why do we use the parentheses in this SQL:
So, the format goes as follows:
Name,location and department of the service of the employees whose name starts with A or B. (A rough translation from French).
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看 SQL Server 中的运算符优先级(您没有指定了这一点,但我想它对于所有 RDBMS 都是相同的)。这意味着 AND(不带括号)
在1 比 OR 结合更紧密之前进行评估。因此,在您的具体情况下,如果没有括号,条件为:
employe.service=service.code_serv ANDEmployee.nom LIKE 'A%'
OR
employe.nom LIKE 'B%'< /code>
1在 SQL 中故意不指定计算顺序,从而允许保证从左到右或优先顺序计算的语言进行更多可能的重新排序。
Take a look at the Operator Precedence in 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 before1 bind more tightly than ORs.So in your specific case, without the parenthesis, the conditions are:
employe.service=service.code_serv AND employe.nom LIKE 'A%'
OR
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.
您可以使用它来指定子句的分组,而不是优先级。 SQL 不允许您指定优先级,因为优化器将为您创建最佳优先级。
将在一个语句中采用两个
OR
条件。因此,如果其中一个为真,则AND
也为真。内部括号不是必需的,但有助于可视化分隔。如果没有外括号,最后一个子句的任何内容也都为 true。
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.
Will take both of the
OR
conditions in one statement. So if either is true then theAND
is true as well. The inner parentheses are not necessary, but help in visualizing the separation.Without the outer parentheses it would allow anything with the final clause as true as well.
还有额外的括号。数学中的规则是添加括号以澄清逻辑。在这种情况下,如果删除所有括号,您将得到错误的答案。你所拥有的是 AND ((b) OR (c))。删除所有括号会将其从 (a OR b) AND (a OR c) 变为 (a AND b) OR c,这是不正确的。
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.