有没有更好的方法来表达以下使用多个可选的查询?当有很多属性时,很快就会出错。
SELECT * WHERE {
BIND(:London AS ?source)
OPTIONAL{ ?source rdfs:label ?o .}
OPTIONAL{?source rdfs:comment ?i . }
}
答案 0 :(得分:2)
实际上,你做得不比这更好。我唯一不同的做法是使用值来指定?source 的值,而不是 bind :
select * where {
values ?source { dbpedia:London }
optional { ?source rdfs:label ?o }
optional { ?source rdfs:comment ?i }
}
如果您愿意稍微处理数据,可以使用查询,其中使用值指定属性:
select * where {
values ?source { dbpedia:London }
values ?property { rdfs:label rdfs:comment }
?source ?property ?value
}
当然,如果你这样做,那么你交换行数的列数,但如果任何属性有多个值,你仍然可能更好。例如,在第一种情况下,如果伦敦只有一个标签和一个评论,那么你得到:
source label commment
------------------------------------
London "the label" "the comment"
但是如果你有两个标签和三个评论,那么你将有六个(=两个×三个)行:
source label commment
------------------------------------
London "label1" "comment1"
London "label1" "comment2"
London "label1" "comment3"
London "label2" "comment1"
London "label2" "comment2"
London "label2" "comment3"
在第二种情况下,如果您有一个标签和一个评论,则有两行:
source property value
------------------------------------
London rdfs:label "label1"
London rdfs:comment "comment1"
但是如果你有两个标签和三个评论,你最终只有五(= 2 + 3)行:
source property value
------------------------------------
London rdfs:label "label1"
London rdfs:label "label2"
London rdfs:comment "comment1"
London rdfs:comment "comment2"
London rdfs:comment "comment3"
因此,如果属性可以有多个值(在DBpedia数据中就是这种情况),那么使用值枚举所需的属性可能不太容易出错(没有可选的块,您可以在同一个地方编写所有属性),并且可以减少需要担心的行数。缺点是您必须处理输出并查看每行中的属性;每个属性值都没有单独的列。