Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql SPARQL查询,选择除匹配项以外的所有内容?_Sql_Sparql - Fatal编程技术网

Sql SPARQL查询,选择除匹配项以外的所有内容?

Sql SPARQL查询,选择除匹配项以外的所有内容?,sql,sparql,Sql,Sparql,我已经习惯于用SPARQL编写常规查询了,但是我在处理更高级的东西时仍然有困难。我最新的问题是试图选择除与where子句匹配的内容之外的所有内容。例如,假设我想找到所有喜欢妻子不喜欢的汽车颜色的丈夫(我正在开发一款专有车型,请原谅这个例子,相信它在真实车型中是有意义的) 我可能会: <bob> <spouse> <alice> <bob> <likes> <red> <alice> <likes> &

我已经习惯于用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>

选择卡尔和埃里克而不是鲍勃的查询是什么?如果您可以在同一查询中选择蓝色和黑色,则可获得额外积分。选择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))}
选择丈夫颜色,其中{丈夫?妻子。?丈夫颜色。可选{妻子?wifecolor过滤器(?wifecolor=?颜色)}过滤器(!BOUND(?wifecolor))}

它至少对eric有效,但我还没有测试carl。

在SPARQL 1.1中有一种更简单、更自然的方法(但它相当于可选/绑定解决方案):

选择丈夫的颜色
在哪里{
丈夫?妻子。
丈夫?肤色。
筛选器不存在{?妻子?颜色}
}

这是传统的解决方案。
SELECT ?husband ?color 
WHERE {
    ?husband <spouse> ?wife .
    ?husband <likes> ?color .
    FILTER NOT EXISTS {?wife <likes> ?color}
}