【发布时间】:2016-02-22 12:29:27
【问题描述】:
当我执行以下查询时:
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: 无法解析表达式项“源”,因为当前记录是 空
如果在“新闻”类中给定的 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 with group by 时跳过空值?
【问题讨论】:
-
您好,“源”属性是什么类型的?字符串、整数还是其他?
-
@MichelaBonizzi 它是字符串类型。
-
好的,非常感谢。
-
您好,我无法重现您的问题,能否请您添加您的课程内容的屏幕截图?或者如果不是问题,则进行数据库测试:)
-
@MichelaBonizzi,您好,这是您要求的内容:“从新闻中选择”的查询结果:sc-cdn.scaleengine.net/i/34821041909318dd7fc23de1e4b65540.png“SELECT count(*) as count, $new_source as name from news LET $new_source = if(eval("source.indexof('Other') === 0"), "Other", source) where country_id = "1110" group by $new_source": sc-cdn.scaleengine.net/i/e842d70e18d9f1252f9587ceed3754e7.png 实际模式:sc-cdn.scaleengine.net/i/a9f327ada1a880ee2d94354045316153.png
标签: orientdb orientdb-2.1