AS 兼容性:“.. from T as a..”适用于 MySQL 和 SQL Server,而不适用于 Oracle? [复制]
Posted
技术标签:
【中文标题】AS 兼容性:“.. from T as a..”适用于 MySQL 和 SQL Server,而不适用于 Oracle? [复制]【英文标题】:AS compatibility: “.. from T as a..” works in MySQL and SQL Server, not Oracle? [duplicate] 【发布时间】:2020-07-19 02:46:14 【问题描述】:我尝试在hackerrank https://www.hackerrank.com/challenges/symmetric-pairs/problem解决这个问题
以下代码适用于 mysql 和 MS SQL Server,但不适用于 Oracle。有没有人告诉我为什么?
with T as (
select x,y, row_number() over (order by x,y) as rnum from functions
)
select distinct a.x,a.y from T as a, T as b
where a.x = b.y and b.x=a.y and a.x<=a.y and a.rnum!=b.rnum
order by a.x,a.y;
似乎如果我删除“AS”,那么它可以在 Oracle 中使用。这是为什么呢?
with T as (
select x,y, row_number() over (order by x,y) as rnum from functions
)
select distinct a.x,a.y from T a, T b
where a.x = b.y and b.x=a.y and a.x<=a.y and a.rnum!=b.rnum
order by a.x,a.y;
【问题讨论】:
【参考方案1】:Oracle 不允许 as
用于表别名。您可以使用此FROM
子句编写查询:
from T a cross join
T b
但是,这很愚蠢,因为您不打算使用cross join
。正如您的查询所写,它将是:
with T as (
select x, y, row_number() over (order by x, y) as rnum
from functions
)
select distinct a.x, a.y
from T a join
T b
on a.x = b.y and b.x = a.y and a.x <= a.y and a.rnum <> b.rnum
order by a.x, a.y;
如果有点难以破译这应该做什么。但我认为它试图让x
/y
组合出现不止一次。如果是这样,请改用count(*)
:
with f as (
select x, y, count(*) over (partition by x, y) as cnt
from functions f
)
select f.x, f.y
from f
where cnt > 1;
这要简单得多,而且性能也应该更好。而且,它应该适用于问题中提到的所有数据库。
【讨论】:
好的,第二部分有效。但是您使用 'count' 编写的最后一个块不起作用。以上是关于AS 兼容性:“.. from T as a..”适用于 MySQL 和 SQL Server,而不适用于 Oracle? [复制]的主要内容,如果未能解决你的问题,请参考以下文章
MT6701磁编码器使用指南,14Bit单圈绝对值,I2C stm32 HAL库读角度,兼容AS5600