当我执行以下查询时:
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 时跳过空值?