我想做点什么
{
SELECT ?page, "A" AS ?type WHERE
{
?s rdfs:label "Microsoft"@en;
foaf:page ?page
}
}
UNION
{
SELECT ?page, "B" AS ?type WHERE
{
?s rdfs:label "Apple"@en;
foaf:page ?page
}
}
但这会产生语法错误。如何在SPARQL中结合两个选择查询?
答案 0 :(得分:10)
你可以像这样结合他们:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT * WHERE
{
{
SELECT ?page ("A" AS ?type) WHERE
{
?s rdfs:label "Microsoft"@en;
foaf:page ?page
}
}
UNION
{
SELECT ?page ("B" AS ?type) WHERE
{
?s rdfs:label "Apple"@en;
foaf:page ?page
}
}
}
(请查看SPARQL validator)
但是我认为在这种情况下根本不需要子查询。例如:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?page ?type WHERE
{
?s foaf:page ?page .
{ ?s rdfs:label "Microsoft"@en . BIND ("A" as ?type) }
UNION
{ ?s rdfs:label "Apple"@en . BIND ("B" as ?type) }
}
答案 1 :(得分:5)
根据@ user205512的回答,这里有一个适用于Virtuoso:
SELECT * {
?s foaf:page ?page .
{
SELECT ?page ("A" AS ?type) {
?s rdfs:label "Microsoft"@en;
foaf:page ?page
}
} UNION {
SELECT ?page ("B" AS ?type) {
?s rdfs:label "Apple"@en;
foaf:page ?page
}
}
}
诀窍就是在?s foaf:page ?page
之外添加额外的UNION
三元组。这显然是多余的,但它似乎避免了Virtuoso错误,这显然是因为你有一个带有子查询的“裸”UNION
。