Postgres“缺少FROM子句条目”查询错误

时间:2017-12-08 18:10:06

标签: sql postgresql

我正在尝试在Postgres中使用此查询:

SELECT ean, count(nif)
FROM produto P, fornece_sec F
WHERE P.ean = F.ean
GROUP BY ​P.ean
HAVING count(nif) > 1;

我收到此错误:

错误:列引用“ean”含糊不清 第1行:选择ean,count(nif)

如果我将查询更改为:

SELECT P.ean, count(nif)
FROM produto P, fornece_sec F
WHERE P.ean = F.ean
GROUP BY ​P.ean
HAVING count(nif) > 1;

出现另一个错误:

错误:表“p”缺少FROM子句条目 第4行:GROUP BY P.ean

这两个表是以这种方式创建的:

create table produto (
ean char(13) not null unique,
design varchar(255) not null,
categoria varchar(80) not null,
forn_primario char(9) not null,
data date not null,
constraint ean_size CHECK (Length(ean)=13),
constraint pk_produto primary key(ean),
constraint fk_produto_categoria foreign key(categoria) references categoria(nome) ON DELETE CASCADE,
constraint fk_produto_fornecedor foreign key(forn_primario) references fornecedor(nif));

create table fornece_sec (
nif char(9) not null,
ean char(13) not null,
constraint pk_fornece_sec primary key(nif, ean),
constraint fk_fornece_sec_fornecedor foreign key(nif) references fornecedor(nif),
constraint fk_fornece_sec_produto foreign key(ean) references produto(ean) ON DELETE CASCADE);

注意:fornece_sec表中的属性nif与产品表中的forn_primario属性相同。

2 个答案:

答案 0 :(得分:0)

这可能与它无关,但可能会尝试明确声明INNER JOIN:

SELECT P.ean, count(nif)
FROM produto P
INNER JOIN fornece_sec F ON P.ean = F.ean
GROUP BY ​P.ean
HAVING count(nif) > 1;

答案 1 :(得分:0)

汤米是对的:

“您的GROUP BY似乎包含unicode,而不是ASCII。也许P是非ASCII字符,看起来像P。”