SPARQL通过变量而不是行数限制查询结果 [英] SPARQL limiting the query result by a variable instead of the number of rows

查看:90
本文介绍了SPARQL通过变量而不是行数限制查询结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说我有以下数据集:

Lets say I have the following data set:

: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关键字,并且我尝试了很多查询,但没有成功.

I want to query the data set to give me back everything of an instance of :AClass, but only for two instances. I know I have to use the LIMIT keyword, and I have tried a lot of queries but with no success.

换句话说,我想回来:

: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行?

How can I limit the result to the number of 2 instances and NOT to number of 2 rows?

推荐答案

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

Use a subquery to select the two things, and then get the rest of the data in an outer query. It always helps to show legal working data that we can test with. The data you've shown isn't actually legal RDF (since it's missing some periods at the ends of lines), but we can easily create a working example. Here's working data, a query, and the results:

@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     |
--------------------------------------------------------------------

这篇关于SPARQL通过变量而不是行数限制查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆