[英]selecting using sparql based on 'triple does not exist'
I need a little help selecting the right triples from my store....我需要一些帮助来从我的商店中选择合适的三元组......
<a> a <type/1> .
<b> a <type/1> .
<c> a <type/1> .
<c> a <type/2> .
i want to select only elements which are type/1 and not type/2我只想 select 元素是 type/1 而不是 type/2
What is the best way to achieve this using a sparql select query?使用 sparql select 查询实现此目的的最佳方法是什么?
am looking for omething like:我正在寻找类似的东西:
select ?a where
{
?a a <type/1> .
!{ ?a a <type/2> }
}
Thanks,谢谢,
:) :)
An alternative SPARQL 1.1 solution is to use MINUS
eg另一种 SPARQL 1.1 解决方案是使用
MINUS
,例如
SELECT ?a
WHERE
{
?a a <type/1> .
MINUS { ?a a <type/2> . }
}
MINUS
subtracts solutions that match its triple pattern from the existing matches. MINUS
从现有匹配项中减去与其三重模式匹配的解决方案。
In most cases using FILTER NOT EXISTS { }
and MINUS { }
are equivalent but beware there are some corner cases where this is not true - see the SPARQL 1.1 specification for some examples of this.在大多数情况下,使用
FILTER NOT EXISTS { }
和MINUS { }
是等价的,但要注意在某些极端情况下这是不正确的 - 请参阅SPARQL 1.1 规范以获取一些示例。
In SPARQL 1.0 that's a bit tricky:在 SPARQL 1.0 中,这有点棘手:
SELECT ?a WHERE {
?a a <type/1>.
OPTIONAL {
?a a ?othertype .
FILTER (?othertype = <type/2>)
}
FILTER (!BOUND(?othertype))
}
The OPTIONAL
clause binds ?othertype
for any ?a
that has <type/2>
, and leaves it unbound for any ?a
that doesn't have it. OPTIONAL
子句将?othertype
绑定到任何具有<type/2>
的?a
?a
上。
The final FILTER
then selects only those rows where ?a
was left unbound.最后的
FILTER
然后只选择?a
未绑定的那些行。
In SPARQL 1.1 it's much easier:在 SPARQL 1.1 中,这要容易得多:
SELECT ?a WHERE {
?a a <type/1>.
FILTER NOT EXISTS { ?a a <type/2> . }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.