0

当我执行以下查询时:

SELECT count(*) as count, $new_source as name 
from news 
LET $new_source = if(eval("source.indexof('Other') === 0"), "Other", source)
where country_id = "111111"
group by $new_source

它抛出一个错误说:

com.orientechnologies.orient.core.exception.OCommandExecutionException:无法解析表达式项“源”,因为当前记录为 NULL

如果在“新闻”类中给定的 country_id 至少有一条记录,则效果很好,但如果给定的 country_id 没有记录,则抛出此错误。

由于我对所有新闻记录使用通用查询,而不考虑国家/地区 ID,因此如果特定国家/地区没有记录,我希望返回空记录集。

我也尝试过使用 orientdb 的 ifnull 函数来跳过空值,如下所示:

SELECT count(*) as count, $new_source as name from news LET $new_source = ifnull(source, 0, if(eval("source.indexof('Other') === 0"), "Other", source)) where country_id = "111111" group by $new_source

但它不工作,并抛出同样的错误。

我正在使用 OrientDb 2.1.8。我不想使用javascript函数并从控制台调用它(如建议here

有什么办法,我可以在使用 if 和 group by 时跳过空值?

4

2 回答 2

1

我试图修改您的查询,也许我找到了解决方案:

select count(*) as count,name
from (
  select if(eval("source.indexof('Other') === 0"), "Other", source) as name 
  from news where country_id = "111111")  
group by name

我没有错误,也没有正确的结果。

在此处输入图像描述

select count(*) as count,name
from (
  select if(eval("source.indexof('Other') === 0"), "Other", source) as name 
  from news where country_id = "1110")  
group by name

在这里正确我有结果

在此处输入图像描述

希望能帮助到你。

于 2016-02-22T19:34:25.517 回答
0

这应该可以解决您的问题:

删除您的索引并使用引擎 Lucene 重新创建另一个全文类型的索引。

在此处输入图像描述

查询你得到这个结果:

在此处输入图像描述

于 2016-02-22T15:39:43.810 回答