将SQL语法从MySQL 5.0转换为Oracle 12c

时间:2018-01-20 21:49:10

标签: mysql sql oracle

我正在尝试将MySQL 5.0视图迁移到Oracle 12c。这是我在MySQL中的语法:

select count(`table1`.`uuid`) AS `Count`,
`table2`.`description` AS `STATUS`,
`table3`.`name` AS `Court` 
from ((`table2` join `table1`) 
join `table3`) 
where ((`table1`.`statusCode` = `table2`.`code`) 
and (`table3`.`uuid` = `table1`.`courtUuid`)) 
group by `table1`.`courtUuid`,`table2`.`description`;

这是我对Oracle 12c的语法:

select count(table1.uuid) AS Count,
table2.description AS STATUS,
table3.NAME as Court
from table2
join table1
on (table1.statuscode = table2.code)
join table3
on (table3.uuid = table1.courtuuid)
group by table1.courtuuid,table2.description;

当我运行Oracle 12c语法时,我得到:

ORA-00979:不是GROUP BY表达式 00979. 00000 - “不是GROUP BY表达式” *原因:
*行动: 行错误:3列:1

但是,如果我将table3.NAME添加为group by的一部分,我会得到866行。 MySQL中的查询产生1025行。

select count(table1.uuid) AS Count,
table2.description AS STATUS,
table3.NAME as Court
from table2
join table1
on (table1.statuscode = table2.code)
join table3
on (table3.uuid = table1.courtuuid)
group by table1.courtuuid,table2.description,table3.NAME;

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

你可能想要:

select count(table1.uuid) AS Count,
       table2.description AS STATUS,
       table3.NAME as Court
from table2 join
     table1
     on table1.statuscode = table2.code join
     table3
     on (able3.uuid = table1.courtuuid
group by table3.name, table2.description;

select中的未聚合列应位于group by中。不幸的是,MySQL有一个(错误的)功能,不需要这种逻辑。

严格来说,你也可以这样做:

select count(table1.uuid) AS Count,
       table2.description AS STATUS,
       max(table3.NAME) as Court
from table2 join
     table1
     on table1.statuscode = table2.code join
     table3
     on table3.uuid = table1.courtuuid
group by table1.courtuuid, table2.description;

这更类似于您的查询,但第一个版本可能就是您想要的。

相关问题