Sparql 查询:查找具有相同属性对象的对象 [英] Sparql query: find objects with same property objects

查看:38
本文介绍了Sparql 查询:查找具有相同属性对象的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个如下所示的数据集:

:person :wantsCD :cd1;:wantsCD :cd2 .:storeA :sellsCD :cd1;出售CD :cd2;出售CD :cd3 .:storeB :sellsCD :cd1;出售CD:cd10;出售CD:cd100.

我有兴趣找到销售 :person 所需的所有 CD 的商店,即 (:storeA).是否可以通过 SPARQL 查询获得此结果?当 :person 想要的 CD 数量在运行时未知时,该查询也应该有效.我试过了:

SELECT DISTINCT ?store ?cd在哪里 {:person :wantsCD?cd.?store :sellsCD ?cd.}

但这会返回 :storeA:storeB,因为 :storeB 也出售 :cd1.

解决方案

解决方案

<块引用>

我有兴趣找到出售所有 CD 的商店 :person想要,即 (:storeA).是否可以使用 SPARQL 获得此结果查询?

是的,诀窍是做一些与所有商店相匹配的事情,然后过滤掉那些商店没有的人想要的CD:

select ?store where {#-- 匹配每家销售任何 CD 的商店.?store :sellsCD []#-- 确保没有该人想要的 CD#-- 商店不卖.过滤器不存在{:person :wantsCD ?cd #-- 没有 :person 想要的 CD过滤器不存在{?store :sellsCD ?cd #-- ?store 不卖.}}}

注意事项

非常感谢您提供示例数据以供使用.除了您使用的 ; 符号之外,就像一个可能使您的生活更轻松的注释,例如:

:person :wantsCD :cd1;:想要CD :cd2

对于同一属性的多个对象,还有一个 , 表示法.您可以将该行写为:

:person :wantsCD :cd1, :cd2

Suppose we have a dataset that looks like this:

:person :wantsCD :cd1; :wantsCD :cd2 .
:storeA :sellsCD :cd1; sellsCD :cd2; sellsCD :cd3 .
:storeB :sellsCD :cd1; sellsCD :cd10; sellsCD :cd100 .

I'm interested in finding the stores that sell all the CD's :person wants, i.e. (:storeA). Is it possible to get this result with a SPARQL query? The query should also work when the number of CD's the :person wants is unknown at runtime. I tried:

SELECT DISTINCT ?store ?cd
WHERE { 
 :person :wantsCD ?cd.
 ?store :sellsCD ?cd.
}

but this returns both :storeA and :storeB, since :storeB also sells :cd1.

解决方案

The Solution

I'm interested in finding the stores that sell all the CD's :person wants, i.e. (:storeA). Is it possible to get this result with a SPARQL query?

Yes, the trick is to do something that matches all the stores, and then filters out those for which there is a CD that the person wants that the store does not have:

select ?store where {
  #-- matches every store that sells any CD.
  ?store :sellsCD []

  #-- make sure there is no CD that the person wants
  #-- that the store does not sell.
  filter not exists { 
    :person :wantsCD ?cd    #-- There is no CD that :person wants
    filter not exists {
      ?store :sellsCD ?cd   #-- that ?store does not sell.
    }
  }
}

Notes

Thank you very much for providing sample data to work with. Just as a note that might make your life a bit easier, in addition to the ; notation that you used in, e.g.:

:person :wantsCD :cd1; :wantsCD :cd2

there's also a , notation for multiple objects of the same property. You can write that line as:

:person :wantsCD :cd1, :cd2

这篇关于Sparql 查询:查找具有相同属性对象的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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