草庐IT

mysql - ORA-00979 : not a GROUP BY expression for Oracle but not valid for MySQL in context of clause difference

coder 2023-10-08 原文

我已经在 Oracle 中运行了这个查询

select studentid, attndmark
from attendance_master m,
     attendance_detail d
where m.attnid = d.attendid
group by studentid

得到错误:

ORA-00979: not a GROUP BY expression

错误很好,我知道 select 子句中列列表的问题。但类似的查询在 MySQL 中有效。

SELECT aff.akey, username
FROM `affiliates` aff,
     affstats ast
WHERE aff.akey = ast.akey
group by aff.akey

我需要一个可以在 RDBMS Oracle/Mysql 和 MSSQL 上运行的查询技巧。

有什么诀窍?

最佳答案

MySQL 是错误的,因为它不符合 SQL 标准(在这种情况下甚至不符合常识)。它允许 SELECT 中的列不是聚合函数的参数并且不在 GROUP BY 中。文档明确指出这些值来自“不确定”行。

顺便说一下,您应该学习正确的显式 JOIN 语法。查询可以写成:

SELECT aff.akey, MAX(username)
FROM affiliates aff JOIN
     affstats ast 
     ON aff.akey=ast.akey
GROUP BY aff.akey;

这将适用于两个数据库。

关于mysql - ORA-00979 : not a GROUP BY expression for Oracle but not valid for MySQL in context of clause difference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34158434/

有关mysql - ORA-00979 : not a GROUP BY expression for Oracle but not valid for MySQL in context of clause difference的更多相关文章

随机推荐