sqlite查询子句中需要括号 [英] Need of parentheses in sqlite query clause

查看:26
本文介绍了sqlite查询子句中需要括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行以下 sqlite3 查询并注意到前两个查询没有给我预期的答案,即 1240969 + 1225691 != 1531026.如果我在 'or' 子句周围使用括号,我会得到预期的结果.这是为什么?

I am running the following sqlite3 queries and notice that the first two queries are not giving me the expected answer, i.e. 1240969 + 1225691 != 1531026. If I use parentheses around the 'or' clauses I get the expected result. Why is this?

sqlite> select count(*) from d where county = "A" or county = "D" and year = "1911";
1240969
sqlite> select count(*) from d where county = "A" or county = "D" and year = "1901";
1225691
sqlite> select count(*) from d where county = "A" or county = "D";
1531026
sqlite> select count(*) from d where (county = "A" or county = "D") and year = "1901";
748015
sqlite> select count(*) from d where (county = "A" or county = "D") and year = "1911";
783011

推荐答案

ANDOR 2 个操作符之间,AND 有优先权,所以这个:

Between the 2 operators AND and OR, AND has precedence, so this:

county = "A" or county = "D" and year = "1911"

评价像

county = "A" or (county = "D" and year = "1911")

所以它不一样:

(county = "A" or county = "D") and year = "1911"

这篇关于sqlite查询子句中需要括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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