我得到了这个oracle错误:
ORA-00979: not a GROUP BY expression
00979. 00000 - "not a GROUP BY expression"
*Cause:
*Action:
Error at Line: 1 Column: 17
运行以下查询时:
SELECT M.EMAIL, M.FNAME, M.LNAME, COUNT(*)
FROM MEMBER M, FRIENDSHIP F
WHERE (M.EMAIL = F.USER1 OR M.EMAIL = F.USER2 ) AND
F.TYPE = 'Family'
GROUP BY M.EMAIL
当我从M.EMAIL
子句中删除除SELECT
之外的所有字段时,它可以正常工作,但只要我添加另一个字段M.FNAME
就会出现同样的错误!
发生了什么事?
答案 0 :(得分:2)
将SELECT
中的所有非汇总列放在GROUP BY
中。另请注意,这将消除相同EMAIL
具有多个FNAME
和LName
的所有行。每个电子邮件-Fname-Lname组合只有一行。
SELECT M.EMAIL, M.FNAME, M.LNAME, COUNT(*)
FROM MEMBER M, FRIENDSHIP F
WHERE (M.EMAIL = F.USER1 OR M.EMAIL = F.USER2 ) AND
F.TYPE = 'Family'
GROUP BY M.EMAIL, M.FNAME, M.LNAME
答案 1 :(得分:1)
尝试以下查询,它将解决您的问题:)
SELECT M.EMAIL, M.FNAME, M.LNAME, COUNT(*)
FROM MEMBER M, FRIENDSHIP F
WHERE (M.EMAIL = F.USER1 OR M.EMAIL = F.USER2 ) AND
F.TYPE = 'Family'
GROUP BY M.EMAIL,M.FNAME,M.LNAME
注意:如果在Select子句中使用聚合函数,我们需要将所有列名按函数放在group by函数值列之外。