Specifying an intersection with multiple values in

2019-08-14 09:33发布

I'm trying to build a query that returns events that have two named participants. The names of these participants are specified using values for the first name and last name. So far, this is the only working solution I have been able to come up with:

SELECT ?event
WHERE {
    ?event con:hasParticipant ?personA .
    ?personA con:hasFirstName "Bob"^^xsd:string .
    ?personA con:hasLastName "Smith"^^xsd:string .
    ?event con:hasParticipant ?personB .
    ?personB con:hasFirstName "The"^^xsd:string .
    ?personB con:hasLastName "Bear"^^xsd:string .
}

...but I'm wondering if there is a better way of doing this?

Any help would be greatly appreciated!

1条回答
Bombasti
2楼-- · 2019-08-14 09:55

This is almost identical to Narrowing down on SPARQL query, except that there are more specific conditions on who you're looking for in this one (in the other one, the task was to find participants, moderators, or guests of honor). The answer there will help. In this case, you can make this a bit simpler. You can separate multiple objects with commas (e.g., ?event hasParticipant ?p1, ?p2). You can use blank nodes to avoid binding variables for the people, and you can (probably) eliminate the datatypes on the strings:

SELECT ?event {
    ?event con:hasParticipant [ con:hasFirstName "Bob" ;
                                con:hasLastName "Smith" ] ,
                              [ con:hasFirstName "The" ;
                                con:hasLastName "Bear" ] .
}
查看更多
登录 后发表回答