gpt4 book ai didi

sql - Count(*) with order by not working on PostgreSQL which works on Oracle

转载 作者:行者123 更新时间:2023-11-29 11:33:16 30 4
gpt4 key购买 nike

下面是适用于 oracle 但不适用于 PostgreSQL 的 Sql 查询。

select count(*) from users where id>1 order by username;

我知道 order by 在此查询中没有任何意义,但仍然是它在 oracle 上工作的原因。以下是 PostgreSQL 上的错误

ERROR: column "users.username" must appear in the GROUP BY clause or be used in an aggregate function
Position: 48

SQLState:42803

PostgreSQL 版本 9.6.3

最佳答案

Oracle's execution plan 所见,行聚合后没有排序,这表明 Oracle 实现的 SQL 引擎忽略了该短语。

为什么它在 PostgreSQL 中不起作用——因为运行 Postgres 的人知道他们在做什么 ;) 开个玩笑,但这个问题对我来说是高度推测的,没有看到 Oracle 与 MySQL 的源代码。更大的问题是 Oracle 和 MySQL 是巧合地允许这样做,还是因为 Oracle 拥有这两者。

最后说明:如果您要问为什么类似的软件应用程序的行为不同,我认为包括您所指的版本也很重要。即使是同一应用程序的不同版本也可能遵循不同的说明。

关于sql - Count(*) with order by not working on PostgreSQL which works on Oracle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45096178/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com