postgresql 在 where 子句中使用“AND”时出现错误“参数必须是布尔类型”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12686193/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 00:22:33  来源:igfitidea点击:

When using "AND" in where clause get error "argument must be type boolean"

sqlpostgresql

提问by Celeritas

Error: argument of AND must be type boolean, not type character varying

错误:AND 的参数必须是布尔类型,而不是类型可变的字符

SELECT 
    partno, 
    count(manufacturer) 
FROM 
    components 
WHERE 
    partno IN (SELECT partno FROM productions 
               WHERE 
                    year = 2005
                AND attr is NULL
              ) 
GROUP BY partno
UNION
SELECT 
    partno, 
    count(manufacturer) 
FROM components 
WHERE 
    partno IN (SELECT partno FROM productions 
               WHERE 
                   year = 2005
               AND attr is NULL
              ) 
GROUP BY partno
) 
AND (
        partno NOT IN (SELECT partno FROM components
    )
); 

The part after the union is to include all partno from components that are not in productions (they should be counted as 0)

union 后面的部分是包括所有不在生产中的组件的partno(它们应该算作0)

采纳答案by davek

You have one bracket too many (after the attr IS NULL) and you have an aggregate function (the countin the second part) without a group by. Do you mean this:

您有一个括号太多(在 之后attr IS NULL)并且您有一个count没有group by. 你的意思是:

select partno, count(manufacturer) 
from components 
where partno in 
(
   select partno from productions where year=2005 and attr is NULL
) 
group by partno

UNION

select partno, count(manufacturer) 
from components 
where partno in 
(
    select partno from productions where year=2005 and attr is NULL
) 
AND partno not in (select partno from components)
group by partno;