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表达式为什么会这样?
答案 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子句没有这样的限制。