【SPARQL】OPTIONAL 子句

用以下数据集作为例子:

@prefix ab: <http://learningsparql.com/ns/addressbook#> .
@prefix d:  <http://learningsparql.com/ns/data#> .

# Richard information
d:i0432 ab:firstName "Richard" . 
d:i0432 ab:lastName  "Mutt" . 
d:i0432 ab:homeTel   "(229) 276-5135" . 
d:i0432 ab:nick      "Dick" .
d:i0432 ab:email     "richard49@hotmail.com" . 

# Cindy information
d:i9771 ab:firstName "Cindy" . 
d:i9771 ab:lastName  "Marshall" . 
d:i9771 ab:homeTel   "(245) 646-5488" . 
d:i9771 ab:email     "cindym@gmail.com" . 

# Craig infomation
d:i8301 ab:firstName "Craig" . 
d:i8301 ab:lastName  "Ellis" . 
d:i8301 ab:workTel   "(245) 315-5486" .
d:i8301 ab:email     "craigellis@yahoo.com" . 
d:i8301 ab:email     "c.ellis@usairwaysgroup.com" .

需要使用OPTIONAL的原因

当我们想要查询 first name、 last name 和 work phone numbers 时,可能会写出以下语句:

PREFIX ab: <http://learningsparql.com/ns/addressbook#> .

SELECT ?first ?last ?workTel
WHERE {
    ?s ab:firstName ?first ;
       ab:lastName ?last ;
       ab:workTel ?workTel .
}

却会发现查询结果中只有 Craig 的信息。这是因为 SPARQL Processor 只会返回能够匹配每一个 triple pattern 的数据,所有只有同时具有 firstName、lastName和workTel的数据才会被命中。

单个条件的OPITIONAL语句

如果我们想把部分条件变成可选的,那么我们可以使用 OPTIONAL 这个关键字:

PREFIX ab: <http://learningsparql.com/ns/addressbook#> .

SELECT ?first ?last ?workTel
WHERE {
    ?s ab:firstName ?first ;
       ab:lastName ?last .
    OPTIONAL { ?s ab:workTel ?workTel . }
}

于是便可以得到结果:

---------------------------------------------
| first    |last          |workTel           |
=============================================
|"Craig"   |"Ellis"       |"(245)315-5486"   |
|"Cindy"   |"Marshall"    |                  |
|"Richard" |"Mutt"        |					 |

多个条件的OPTIONAL语句

在数据中,只有 Richard 有 ab:nick 属性,只有 Craig 有 ab:workTel 属性,对他们做一下查询

注意区分A与B两种语句:

A:

PREFIX ab: <http://learningsparql.com/ns/addressbook#> .

SELECT ?first ?last ?workTel ?nick
WHERE {
    ?s ab:firstName ?first ;
       ab:lastName ?last .
    OPTIONAL { 
        ?s ab:workTel ?workTel ;
           ab:nick ?nick .
    }
}

执行结果:

------------------------------------------
| first     | last       | workTel | nick | 
=========================================== 
| "Craig"   | "Ellis"    |         |      | 
| "Cindy"   | "Marshall" |         |      | 
| "Richard" | "Mutt"     |         |      | 
-------------------------------------------

由于没有任何一个subject同时具有workTelnick 属性,因此看不到结果。

B:

PREFIX ab: <http://learningsparql.com/ns/addressbook#> .

SELECT ?first ?last ?workTel ?nick
WHERE {
    ?s ab:firstName ?first ;
       ab:lastName ?last .
    OPTIONAL { ?s ab:workTel ?workTel . }
    OPTIONAL { ?s ab:nick ?nick . }
}

执行结果:

-----------------------------------------------------
| first     | last       | workTel          | nick   | 
====================================================== 
| "Craig"   | "Ellis"    | "(245) 315-5486" |        | 
| "Cindy"   | "Marshall" |                  |        | 
| "Richard" | "Mutt"     |                  | "Dick" |
------------------------------------------------------

我们将 workTelnick 均变成了可选属性,因此这些数据都被查询了出来。

The OPTIONAL keyword is especially helpful for exploring a new dataset that you’re unfamiliar with. For example, if you see that it assigns certain property values to certain resources, remember that not all resources of that type may have those properties assigned to them.
Putting triple patterns inside of OPTIONAL sections lets you retrieve values if they’re there without interfering with the retrieval of related data if those values aren’t there.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值