我越来越舒服SPARQL编写定期查询,但我仍然有票友东西的麻烦。我的最新的问题尝试选择不同的东西,where子句相匹配的一切。例如,假设我要查找所有谁喜欢汽车的颜色,他们的妻子不喜欢的丈夫(我工作的一个专有模式,所以借口的例子,只是相信它使真正意义上的模型)。

我可能有:

<bob> <spouse> <alice>
<bob> <likes> <red>
<alice> <likes> <red>
<carl> <spouse> <dorothy>
<carl> <likes> <blue>
<dorothy> <likes> <yellow>
<eric> <spouse> <fannie>
<eric> <likes> <black>

那是什么选择卡尔和Eric,而不是鲍勃查询?奖励积分,如果你可以选择蓝色和黑色在相同的查询。鲍勃选择将是简单地:

select ?husband ?color where {?husband <spouse> ?wife . ?husband <likes> ?color . ?wife <likes> ?color}

我正在寻找的是:

select ?husband ?color where {?husband <spouse> ?wife . ?husband <likes> ?color . NOT (?wife <likes> ?color)}

但很明显,这是错误的。那么,什么是正确的?

有帮助吗?

解决方案

一个正确答案我通过其他渠道发现的是这样的:

select ?husband ?color where {?husband <spouse> ?wife . ?husband <likes> ?color . OPTIONAL {?wife <likes> ?wifecolor FILTER (?wifecolor = ?color)} FILTER (!BOUND(?wifecolor))}

有至少适用于埃里克,但是我没有测试卡尔。

其他提示

有一个更简单的和更自然的方式来做到这一点在SPARQL 1.1(但它是等效于OPTIONAL / BOUND溶液):

SELECT ?husband ?color 
WHERE {
    ?husband <spouse> ?wife .
    ?husband <likes> ?color .
    FILTER NOT EXISTS {?wife <likes> ?color}
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top