选择小于site_name的site_acr

时间:2014-07-19 23:07:21

标签: sql postgresql

名称(char)和站点面积(int)的列表,其中site site(int)小于First Ave(char)站点的面积

我输入此内容并且无效

select ds.dev_site_name, ds.site_acreage
from development_site ds
where ds.site_acreage<ds.site_acreage(select ds.site_acreage where ds.dev_site_name='First Ave')
order by ds.site_acreage desc

并且只返回4个值中的2个

select ds.dev_site_name, ds.site_acreage
from development_site ds
where ds.dev_site_name<'First Ave'
order by ds.site_acreage desc

2 个答案:

答案 0 :(得分:0)

您可以通过在子查询中检索First Ave的面积来选择面积小于First Ave的所有网站

select ds.dev_site_name, 
ds.site_acreage 
from development_site ds 
where ds.site_acreage < (
    select site_acreage from development_site
    where dev_site_name = 'First Ave'
    limit 1
)
order by ds.site_acreage desc

答案 1 :(得分:0)

您根本不需要表别名,因为您只在每个(子)查询中处理单个表:

SELECT dev_site_name, site_acreage
FROM   development_site
WHERE  site_acreage < (
   SELECT site_acreage
   FROM   development_site
   WHERE  dev_site_name = 'First Ave'
   )
ORDER  BY site_acreage DESC;

如果dev_site_name定义为UNIQUE,则子查询中不需要LIMIT 1
如果它不是唯一的,那么您的查询是不明确的,您需要使用dev_site_name = 'First Ave'从一组行中选择哪个行。