有关Oracle中GROUP BY子句的错误

时间:2013-09-13 04:25:08

标签: sql database oracle

我得到了这个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就会出现同样的错误!

发生了什么事?

2 个答案:

答案 0 :(得分:2)

SELECT中的所有非汇总列放在GROUP BY中。另请注意,这将消除相同EMAIL具有多个FNAMELName的所有行。每个电子邮件-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函数值列之外。