简体   繁体   English

sparql如何子查询到另一个查询

[英]sparql how to sub query into another query

I have this data 我有这个数据

@prefix : <http://test.example/> .

:alice :likes :beethoven.
:alice :likes :verdi.
:sofia :likes :beethoven.
:sofia :likes :verdi.
:sofia :likes :rossini.
:ania :likes :verdi.
:ania :likes :beethoven.
:ania :likes :david.
:david :likes :ania.
:david :likes :beethoven.
:david :likes :verdi.
:antonino :likes :verdi.
:antonino :likes :mozart.
:weirdo :likes :katyperry.
:beethoven a :recommendable.
:verdi a :recommendable.
:rossini a :recommendable.
:katyperry a :recommendable.
:mozart a :recommendable.

and I make a query to get the users that likes the same items as a specific user 然后我查询以获取与特定用户喜欢相同项目的用户

select ?anotherUser (COUNT(?anotherItem) as ?countOfItems) WHERE {
  values ?user {:ania}
  ?anotherUser :likes ?anotherItem.
  filter (?anotherUser != ?user)
  filter exists {?user :likes ?anotherItem}
}group by ?anotherUser
order by desc(?countOfItems)

now I want to get the items that these users like (but of course without the items that they share with that specific user ( :ania )) 现在,我想获取这些用户喜欢的项目(但当然没有他们与该特定用户( :ania )共享的项目)

I know I have to include a query inside the other, I tried a lot myself but no success, could you help please? 我知道我必须在另一个内部包含一个查询,我自己做了很多尝试,但没有成功,请您能帮忙吗?

now I want to get the items that these users like (but of course without the items that they share with that specific user (:ania)) 现在,我想获取这些用户喜欢的项目(但当然没有他们与该特定用户共享的项目(:ania))

I know I have to include a query inside the other, I tried a lot myself but no success, could you help please? 我知道我必须在另一个内部包含一个查询,我自己做了很多尝试,但没有成功,请您能帮忙吗?

I believe the main thing to keep in mind is that subqueries are evaluated foremost ie the query is evaluated from the innermost first, to the outermost. 我认为要记住的主要事情是,首先查询子查询,即从最里面的到最外面的查询进行查询。

So the following query: 所以下面的查询:

prefix : <http://test.example>
select distinct ?user ?anotherUser ?item WHERE {
  ?anotherUser :likes ?item.
  filter not exists {?user :likes ?item}
  {select ?user ?anotherUser where {
    values ?user {:ania}
    ?anotherUser :likes ?anotherItem.
    filter (?anotherUser != ?user)
    filter exists {?user :likes ?anotherItem}
  }}
}

will produce the result: 将产生结果:

----------------------------------
| user  | anotherUser | item     |
==================================
| :ania | :sofia      | :rossini |
| :ania | :antonino   | :mozart  |
| :ania | :david      | :ania    |
----------------------------------

which is what you asked for right? 您要求的是对的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM