sqlite查询子句中需要括号

时间:2018-11-01 20:39:31

标签: sqlite

我正在运行以下sqlite3查询,请注意前两个查询没有给我期望的答案,即1240969 + 1225691!=1531026。如果在“或”子句周围使用括号,则会得到预期的结果。为什么会这样?

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

1 个答案:

答案 0 :(得分:2)

在两个运算符ANDOR之间,AND具有优先权,因此:

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

评估类似

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

因此与以下内容不同:

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