我正在使用此查询:
select city, length(city)
from station
where length(city) = (select max(length(city)) from station )
OR length(city) = (select min(length(city)) from station)
order by city asc;
并且当我将LIMIT函数添加到子查询中时,因为从该选择中我只需要一个结果:
select city, length(city)
from station
where length(city) = (select max(length(city)) from station limit 1)
OR length(city) = (select min(length(city)) from station limit 1)
order by city asc;
然后我出现错误-
ORA-00907: missing right parenthesis
任何知道我在该函数中犯错的人。我使用Oracle,我尝试使用rownum,但没有帮助。
[使用max()
时不需要limit
:
select city, length(city)
from station
where length(city) = (select max(length(city))
from station
) OR
length(city) = (select min(length(city))
from station
)
order by city asc;
但是,limit
在Oracle
中不支持。
Oracle不支持limi [n]t
语法。等效为fetch first [n] rows only
(从Oracle 12c开始可用)。
但最重要的是:保证每个子查询仅返回一个记录,因此不需要limit
。
我还建议重写查询以使用rank()
,这避免了对多个子查询的需求,并且在我看来,这使逻辑更清晰:
select city, length_city
from (
select
city,
lenght(city) length_city,
rank() over(order by length(city)) rn_asc,
rank() over(order by length(city) desc) rn_desc
from station
) t
where rn_asc = 1 or rn_desc = 1
[如果您想要顶部/底部关系并且只想要其中之一,则可以向排名功能添加其他排序条件:例如,order by length(city), city
为您提供按字母顺序排列的第一个城市重复项。