带有AND / OR运算符的错误PostgreSQL请求

时间:2014-07-21 09:19:07

标签: sql postgresql

我的postresql请求有问题吗?:

SELECT DISTINCT COUNT(email) FROM data 
WHERE email != '' AND gender='mme' OR gender='mlle' 
AND yearofbirth BETWEEN 1973 AND 2003

我们可以根据需要设置'AND'和'OR'运算符吗? 由于声明'AND yearofbirth BETWEEN 2003和1973'不起作用,请求结果是:

SELECT DISTINCT COUNT(email) FROM data 
WHERE email != '' AND gender='mme' OR gender='mlle'

...

同样的问题:

SELECT DISTINCT COUNT(email) FROM data 
WHERE email != '' AND gender='mme' OR gender='mlle' 
AND yearofbirth < 2003 AND yearofbirth > 1973

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

SELECT DISTINCT COUNT(email) 
     FROM data WHERE email != '' AND  ( gender='mme' OR gender='mlle') AND 
         yearofbirth BETWEEN 1973 AND 2003

答案 1 :(得分:0)

在这种情况下,显然不需要{p> SELECT DISTINCT。您的查询被表述为没有GROUP BY的聚合查询,因此它只会返回一行。我怀疑你真的想要SELECT COUNT(DISTINCT),一个完全不同的野兽。

我也不确定你在WHERE条款中的意图。我怀疑这个:

SELECT COUNT(DISTINCT email)
FROM data 
WHERE email <> '' AND
      (gender IN 'mme', 'mlle') AND 
      yearofbirth BETWEEN 1973 AND 2003;

虽然没有括号,但你有这个:

SELECT COUNT(DISTINCT email)
FROM data 
WHERE (email <> '' AND gender = 'mme') OR
      (gender = 'mlle' AND yearofbirth BETWEEN 1973 AND 2003);