SPARQL查詢語句詳解(不懂的可以樓下評論,看到秒回)
阿新 • • 發佈:2018-12-22
SPARQL
-
簡單家庭關聯網路(知識圖譜)
-
用rdf描述這個網路
<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#' xmlns:info='http://zy.example.com/info#' xmlns:rel='http://zy.example.com/rel#'> <rdf:Description rdf:about="http://zy.example.com/person/Tom"> <info:name>Tom</info:name> <info:job
-
簡單查詢
查詢語句
prefix info: <http://zy.example.com/info#> prefix rel: <http://zy.example.com/rel#> select ?person ?age where{ ?person info:age ?age }
查詢結果
----------------------------------------------- | person | age | =============================================== | <http://zy.example.com/person/Cherry> | 8 | | <http://zy.example.com/person/Tom> | 56 | | <http://zy.example.com/person/Jim> | 28 | -----------------------------------------------
-
過濾查詢(FILTER, OPTIONAL, ORDERBY)
查詢語句
prefix info: <http://zy.example.com/info#> prefix rel: <http://zy.example.com/rel#> select ?name ?age ?job where{ ?person info:name ?name. ?person info:age ?age. filter(?age <= 30). optional{?person info:job ?job}. } orderby(?age)
查詢結果
--------------------------------- | name | age | job | ================================= | "Cherry" | 8 | | | "Jim" | 28 | "programmer" | ---------------------------------
-
聚合函式(COUNT, SUM, MIN, MAX, AVG)
查詢語句
prefix info: <http://zy.example.com/info#> prefix rel: <http://zy.example.com/rel#> select (AVG(?age) as ?AvgAge) where{ ?person info:age ?age }
查詢結果
------------------------------- | AvgAge | =============================== | 30.666666666666666666666666 | -------------------------------
-
聯合查詢(UNION)
查詢語句
prefix info: <http://zy.example.com/info#> prefix rel: <http://zy.example.com/rel#> select ?name ?age ?job where{ { ?person info:name ?name. optional{?person info:job ?job}. ?person info:age ?age. filter(?age >= 30) } union { ?person info:name ?name. optional{?person info:job ?job}. ?person info:age ?age. filter(?age <=28) } }
查詢結果
--------------------------------- | name | age | job | ================================= | "Tom" | 56 | "worker" | | "Jim" | 28 | "programmer" | | "Cherry" | 8 | | ---------------------------------
-
子查詢
查詢語句
prefix info: <http://zy.example.com/info#> prefix rel: <http://zy.example.com/rel#> select ?grandperson ?person where{ ?grandperson rel:fatherof ?sonperson. { select ?sonperson ?person where{ ?sonperson rel:fatherof ?person. } } }
查詢結果
------------------------------------------------------------------------------ | grandperson | person | ============================================================================== | <http://zy.example.com/person/Tom> | <http://zy.example.com/person/Cherry> | ------------------------------------------------------------------------------