gpt4 book ai didi

rdf - SPARQL 通过变量而不是行数来限制查询结果

转载 作者:行者123 更新时间:2023-12-05 09:21:27 24 4
gpt4 key购买 nike

假设我有以下数据集:

:a  rdf:type      :AClass
:a :hasName "a"^^xsd:string
:a :hasProperty :xa
:a :hasProperty :ya
:a :hasProperty :za

:b rdf:type :AClass
:b :hasName "b"^^xsd:string
:b :hasProperty :xb
:b :hasProperty :yb

:c rdf:type :AClass
:c :hasName "c"^^xsd:string
:c :hasProperty :xc

我想查询数据集以返回:AClass 实例的所有内容,但仅限于两个实例。我知道我必须使用 LIMIT 关键字,而且我尝试了很多查询但没有成功。

换句话说,我想取回这个:

:a  :hasName      "a"^^xsd:string
:a :hasProperty :xa
:a :hasProperty :ya
:a :hasProperty :za

:b :hasName "b"^^xsd:string
:b :hasProperty :xb
:b :hasProperty :yb

如何将结果限制为 2 个实例的数量而不是 2 行的数量?

最佳答案

使用子查询来选择这两个东西,然后在外部查询中获取其余数据。它总是有助于显示我们可以测试的合法工作数据。您显示的数据实际上不是合法的 RDF(因为它在行尾缺少一些句点),但我们可以轻松创建一个工作示例。这是工作数据、查询和结果:

@prefix : <urn:ex:>

:a a :AClass .
:a :hasName "a" .
:a :hasProperty :xa .
:a :hasProperty :ya .
:a :hasProperty :za .

:b a :AClass .
:b :hasName "b" .
:b :hasProperty :xb .
:b :hasProperty :yb .

:c a :AClass .
:c :hasName "c" .
:c :hasProperty :xc .
prefix : <urn:ex:>

select ?s ?p ?o {
#-- first, select two instance of :AClass
{ select ?s { ?s a :AClass } limit 2 }

#-- then, select all the triples of
#-- which they are subjects
?s ?p ?o
}
--------------------------------------------------------------------
| s | p | o |
====================================================================
| :a | <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> | :AClass |
| :a | :hasName | "a" |
| :a | :hasProperty | :xa |
| :a | :hasProperty | :ya |
| :a | :hasProperty | :za |
| :b | <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> | :AClass |
| :b | :hasName | "b" |
| :b | :hasProperty | :xb |
| :b | :hasProperty | :yb |
--------------------------------------------------------------------

关于rdf - SPARQL 通过变量而不是行数来限制查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32309813/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com