【问题标题】:Couchbase - Case with subqueryCouchbase - 带有子查询的案例
【发布时间】:2022-08-05 23:48:10
【问题描述】:

我要写一个有子查询的案例,如下所示,但我既不能输出也不能出错:

select
case
when f.resGeo.isRural = true 
  then (select g.ID as geo_id
    from bktsample.scpPC.GeoInfo g 
    where g.PROVINCE_ID = f.resGeo.province.id 
     and g.CITYES_ID = f.resGeo.countie.id
     and g.PART_ID = f.resGeo.part.id
     and g.CITYORCOUNTRY_ID = f.resGeo.countie.id
     and g.VILLAGE_ID = f.resGeo.village.id)
when f.resGeo.isRural = false
  then (select g.ID 
    from bktsample.scpPC.GeoInfo g 
    where g.PROVINCE_ID = f.resGeo.province.id 
     and g.CITYES_ID = f.resGeo.countie.id
     and g.PART_ID = f.resGeo.part.id
     and g.CITYORCOUNTRY_ID = f.resGeo.countie.id) 
end as geo_id

from bktsample.scpPC.Family f;

PS:GEO 是我的收藏,scpPC 是我的范围,bktsample 是我的桶。

  • 我对“既没有输出也没有错误”感到困惑。当您执行此查询时,一定发生了一些事情。是不是超时了?您创建了哪些索引?
  • 我没有得到任何输出,就像输出:[]
  • 我看到:查询返回 0 个结果

标签: subquery case couchbase n1ql


【解决方案1】:

Family 集合中的每个文档都应该返回一个 geo_id 文档(空数组或 ID 对象)

对您的查询的小改动:

CREATE INDEX ix1 ON bktsample.scpPC.Family(resGeo.province.id, resGeo.countie.id, resGeo.part.id, resGeo.countie.id, resGeo.village.id, resGeo.isRural);
CREATE INDEX ix2 ON bktsample.scpPC.GeoInfo(PROVINCE_ID, CITYES_ID, PART_ID, CITYORCOUNTRY_ID, VILLAGE_ID, ID);

SELECT
    (SELECT g.ID AS geo_id
     FROM bktsample.scpPC.GeoInfo AS g
     WHERE g.PROVINCE_ID = f.resGeo.province.id
           AND g.CITYES_ID = f.resGeo.countie.id
           AND g.PART_ID = f.resGeo.part.id
           AND g.CITYORCOUNTRY_ID = f.resGeo.countie.id
           AND (f.resGeo.isRural == false OR g.VILLAGE_ID = f.resGeo.village.id))
    ) AS geo_ids
FROM bktsample.scpPC.Family f
WHERE f.resGeo.province.id IS NOT NULL; 

【讨论】:

    猜你喜欢
    • 2014-04-29
    • 1970-01-01
    • 2014-11-10
    • 2014-11-08
    • 1970-01-01
    • 2019-06-26
    • 1970-01-01
    • 2013-06-13
    • 2020-05-23
    相关资源
    最近更新 更多