簡體   English   中英

SPARQL查詢,選擇除匹配之外的所有內容?

[英]SPARQL Query, select everything except things that match?

我很樂意在SPARQL中編寫常規查詢,但我仍然遇到了更高級的問題。 我最新的問題是嘗試選擇除了匹配where子句的東西之外的所有東西。 例如,假設我想找到所有喜歡他們的妻子不喜歡的汽車顏色的丈夫(我正在研究專有模型,請原諒這個例子並且相信它在真實模型中是有意義的)。

我可能有:

<bob> <spouse> <alice>
<bob> <likes> <red>
<alice> <likes> <red>
<carl> <spouse> <dorothy>
<carl> <likes> <blue>
<dorothy> <likes> <yellow>
<eric> <spouse> <fannie>
<eric> <likes> <black>

什么是選擇carl和eric的查詢,但不是bob? 如果您可以在同一查詢中選擇藍色和黑色,則可獲得獎勵積分。 選擇鮑勃只是:

select ?husband ?color where {?husband <spouse> ?wife . ?husband <likes> ?color . ?wife <likes> ?color}

我正在尋找的是:

select ?husband ?color where {?husband <spouse> ?wife . ?husband <likes> ?color . NOT (?wife <likes> ?color)}

但顯然這是錯的。 什么是對的?

我通過其他來源找到的一個正確答案是這樣的:

select ?husband ?color where {?husband <spouse> ?wife . ?husband <likes> ?color . OPTIONAL {?wife <likes> ?wifecolor FILTER (?wifecolor = ?color)} FILTER (!BOUND(?wifecolor))}

它至少適用於埃里克,但我沒有測試卡爾。

在SPARQL 1.1中有一種更簡單,更自然的方法(但它相當於OPTIONAL / BOUND解決方案):

SELECT ?husband ?color 
WHERE {
    ?husband <spouse> ?wife .
    ?husband <likes> ?color .
    FILTER NOT EXISTS {?wife <likes> ?color}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM