You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Wikidata SPARQL技术问题:如何排除属性P921返回关系?

SPARQL查询排除属性P921的关系

你编写的原查询如下:

#Cats
SELECT ?item ?itemLabel ?prop 
WHERE 
{
  ?item ?prop wd:Q7206.
  filter not exists {?prop **FILTER** wdt:P921 }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } 
# Helps get the label in your language, if not, then en language
}

要实现排除关系为P921的结果,有两种简洁正确的修改方式:

方式一:直接使用不等于过滤

把原查询中的filter not exists块替换为直接过滤条件,写法更直观:

#Cats
SELECT ?item ?itemLabel ?prop 
WHERE 
{
  ?item ?prop wd:Q7206.
  FILTER (?prop != wdt:P921)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } 
}

方式二:修正filter not exists内的判断逻辑

如果坚持使用filter not exists,需要将FILTER位置替换为=,让子查询判断?prop是否等于wdt:P921,不存在该匹配时保留结果:

#Cats
SELECT ?item ?itemLabel ?prop 
WHERE 
{
  ?item ?prop wd:Q7206.
  FILTER NOT EXISTS {?prop = wdt:P921 }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } 
}

两种方式都能达成需求,第一种写法更简洁高效,推荐使用。

内容的提问来源于stack exchange,提问作者cuneyttyler

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 13:05:55