SPARQL:从多个比赛中选择三分之一? [英] SPARQL: Choose one triple from multiple match?

查看:114
本文介绍了SPARQL:从多个比赛中选择三分之一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对SPARQL查询有一种有趣的问题.我要查询三元组,并且我有多个匹配项-也许是2个.但是我只需要代码选择一个即可.哪一个绝对不重要. 例如:我正在查询一本5岁的书,它在数据库中找到2本书,但是我只需要保存一个变量即可(与哪本书无关). 在SPARQL中甚至有可能吗?如果您需要其他任何信息,请告诉我.预先感谢大家的宝贵时间! 输入数据库的更改是我要做的最后一件事.

I have a kind of interesting problem with SPARQL query. I am querying for a triple and I have multiple match - maybe 2. But I need the code to choose only one. It absolutely doesn't matter which one. For example: I am querying for a book for 5-years old and it founds 2 books in the database but I need to have only one saved in variable (doesn't matter which one). Is this even possible in SPARQL? Please let me know if you need any other information. Thank you all in advance for your time! The change in input database is the last thing I want to do.

示例:

<Book rdf:ID = 0001>
<Book.Title>Title1</Book.Title>
<Book.For>5 years old</Book.For>
<Book.Date>2000</Book.Date>
</Book>

<Book rdf:ID = 0002>
<Book.Title>Title2</Book.Title>
<Book.For>5 years old</Book.For>
<Book.Date>2005</Book.Date>
</Book>

Construct {?NewDatabase Example_of_book_for_5_years_old ?Title .}
Where {?Example Book.For "5 years old" .
       ?Example Book.Title ?Title .
      };

两本书都匹配Where子句,但是我只需要一本就可以在新的数据库输出中使用.没关系.

Both books match the Where clause but I need only one to be in the new database output. Doesn't matter which one.

推荐答案

不考虑示例查询中的明显错误,解决方案是使用SELECT而不是CONSTRUCT.前者可以限制结果的数量.

Not considering the apparent errors in your example query, the solution is to use SELECT instead of CONSTRUCT. The former allows you to limit the number of results.

如果您完全需要三元组,则可以使用有限的SELECT作为子查询

If you totally need a triple as result, you can use limited SELECT as a subquery

要从CONSTRUCT查询中仅选择一个三元组,可以像SELECT

To select only one triple from a CONSTRUCT query you can use the LIMIT clause just like with SELECT

Construct { ?Example <urn:Book:Title> ?Title }
Where {
   ?Example <urn:Book:For> "5 years old" .
   ?Example <urn:Book:Title> ?Title .
} 
limit 1

这将选择第一个结果(顺序不确定).

This will select the first result (order undetermined).

不,由 SPARQL语法判断,不是可以在SPARQL更新中使用LIMIT子句.

And no, the judging by the SPARQL Grammar, it is not possible to use the LIMIT clause with SPARQL Updates.

这篇关于SPARQL:从多个比赛中选择三分之一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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