6

错误: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
    )
); 

union 之后的部分是包含所有不在生产中的组件的 partno(它们应该计为 0)

4

1 回答 1

4

您有一个括号太多(在 之后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; 
于 2012-10-02T07:38:23.147 回答