2008-12-17 193 views
3

我在使用SPARQL编写定期查询时感到很舒服,但我仍然遇到了更多的技术问题。我最近的问题是试图选择除了与where子句匹配的东西外的所有东西。比如说,我想找到所有喜欢他们的妻子不喜欢的汽车颜色的丈夫(我正在开发一个专有模型,所以请原谅这个例子,并相信它在真实模型中是有意义的)。SPARQL查询,选择除匹配的东西以外的所有东西?

我可能有:

<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)} 

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

回答

4

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

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

它至少当埃里克,但我还没有考卡尔。

+0

这是传统的解决方案。 – tialaramex 2009-06-29 22:45:30

2

有做到在SPARQL 1.1更简单,更自然的方式(但它是相当于可选/必然会溶液):

SELECT ?husband ?color 
WHERE { 
    ?husband <spouse> ?wife . 
    ?husband <likes> ?color . 
    FILTER NOT EXISTS {?wife <likes> ?color} 
} 
相关问题