Oracle错误消息:在添加另一个'选择'时不是GROUP BY表达式条款

时间:2017-06-20 10:41:06

标签: sql oracle

select title, Forename AS "Firstname", Surname AS "Lastname", COUNT 
(VISIT.Vet_Id) AS "Visits"
from VISIT, VET
where VISIT.Vet_Id = VET.Vet_Id
group by title, Surname,VISIT.VET_Id
order by title, Surname, VISIT.VET_Id;

当执行上面的代码时,我收到一条错误消息:虽然当我删除Forename AS" Firstname"

时它不是GROUP BY表达式

为什么会这样?

2 个答案:

答案 0 :(得分:0)

只需在SELECT中的GROUP BY中添加非汇总列:

select title, Forename AS "Firstname", Surname AS "Lastname",
       COUNT(vi.Vet_Id) AS "Visits"
from VISIT vi join
     VET v
     on vi.Vet_Id = v.Vet_Id
group by title, Forename, Surname
order by title, Forename, Surname;

并且从不FROM子句中使用逗号。 始终使用正确的,明确的JOIN语法。您还应该对所有列引用进行限定。

答案 1 :(得分:0)

对于Oracle数据库,我们必须在我们在Select子句中使用的Group by子句中添加所有非聚合字段,否则会出错。在mysql的情况下,group by子句没有这样的限制。