OrientDB SQL 递归性
OrientDB SQL recursivness
我有一个工作查询,returns 汤姆·汉克斯的所有合作演员(基本上是 朋友的朋友 问题)。
SELECT COUNT(name) as name FROM (
SELECT expand(out('ACTS_IN').in('ACTS_IN')) FROM Actor WHERE name = 'Tom Hanks'
) WHERE name <> 'Tom Hanks'
现在我正在尝试构建一个查询,该查询会为我提供合作演员的合作演员(朋友的朋友的朋友)
我的查询,未完成
SELECT count(DISTINCT(name)) as coactor FROM (
SELECT expand(out('ACTS_IN').in('ACTS_IN').out('ACTS_IN').in('ACTS_IN').out('ACTS_IN').in('ACTS_IN')) FROM Person WHERE name = 'Tom Hanks'
)
WHERE name <> 'Tom Hanks'
问题是,你能不能像这样堆叠 .in 和 .out:
expand(out('ACTS_IN').in('ACTS_IN').out('ACTS_IN').in('ACTS_IN').out('ACTS_IN').in('ACTS_IN'))
在您的情况下,您可以使用 TRAVERSE()
函数来检索边 ACTS_IN
.
链接的所有记录
结构:
create class Person extends V
create class Movie extends V
create class acts_In extends E
create class directed extends E
create class friend extends E
create class rated extends E
create property Person.name String
create property Person.surname String
create property Movie.title String
create vertex Person set name="Tom", surname="Hanks"
create vertex Person set name="Robin", surname="Wright"
create vertex Person set name="Helen", surname="Hunt"
create vertex Person set name="Robert", surname="Zemeckis"
create vertex Person set name="Russell", surname="Crowe"
create vertex Person set name="Ben", surname="Affleck"
create vertex Person set name="Kevin", surname="Macdonald"
create vertex Person set name="John"
create vertex Person set name="Mark"
create vertex Person set name="Paul"
create vertex Person set name="Mel", surname="Gibson"
create vertex Person set name="Nancy", surname="Meyers"
create vertex Movie set title="Forrest Gump"
create vertex Movie set title="Cast Away"
create vertex Movie set title="State of Play"
create vertex Movie set title="What Women Want"
create edge acts_In from (select from Person where name="Tom" and surname="Hanks") to (select from Movie where title="Forrest Gump")
create edge acts_In from (select from Person where name="Tom" and surname="Hanks") to (select from Movie where title="Cast Away")
create edge acts_In from (select from Person where name="Robin" and surname="Wright") to (select from Movie where title="Forrest Gump")
create edge acts_In from (select from Person where name="Robin" and surname="Wright") to (select from Movie where title="State of Play")
create edge acts_In from (select from Person where name="Helen" and surname="Hunt") to (select from Movie where title="Cast Away")
create edge acts_In from (select from Person where name="Helen" and surname="Hunt") to (select from Movie where title="What Women Want")
create edge acts_In from (select from Person where name="Mel" and surname="Gibson") to (select from Movie where title="What Women Want")
create edge acts_In from (select from Person where name="Russell" and surname="Crowe") to (select from Movie where title="State of Play")
create edge acts_In from (select from Person where name="Ben" and surname="Affleck") to (select from Movie where title="State of Play")
create edge friend from (select from Person where name="Mel" and surname="Gibson") to (select from Person where name="Helen" and surname="Hunt")
create edge friend from (select from Person where name="Ben" and surname="Affleck") to (select from Person where name="Russell" and surname="Crowe")
create edge directed from (select from Movie where title="What Women Want") to (select from Person where name="Nancy" and surname="Meyers")
create edge directed from (select from Movie where title="Cast Away") to (select from Person where name="Robert" and surname="Zemeckis")
create edge directed from (select from Movie where title="Forrest Gump") to (select from Person where name="Robert" and surname="Zemeckis")
create edge directed from (select from Movie where title="State of Play") to (select from Person where name="Kevin" and surname="Macdonald")
create edge rated from (select from Movie where title="What Women Want") to (select from Person where name="Paul")
create edge rated from (select from Movie where title="Cast Away") to (select from Person where name="John")
create edge rated from (select from Movie where title="Forrest Gump") to (select from Person where name="Mark")
create edge rated from (select from Movie where title="State of Play") to (select from Person where name="John")
查询 1:检索 'Tom Hanks' 的所有 actors/co-actors/ecc...(除了他自己)
SELECT expand(DISTINCT) FROM (SELECT DISTINCT(@rid) FROM (TRAVERSE BOTH('ACTS_IN')
FROM (SELECT FROM Person WHERE name = 'Tom' AND surname = 'Hanks') MAXDEPTH 4)
WHERE name <> 'Tom' and surname <> 'Hanks')
查询 2:检索 'Tom Hanks' 的 actors/co-actors/ecc... 的计数(除了他自己)
SELECT COUNT(DISTINCT(@rid)) AS co_actors FROM (TRAVERSE BOTH('ACTS_IN')
FROM (SELECT FROM Person WHERE name = 'Tom' AND surname = 'Hanks') MAXDEPTH 4)
WHERE name <> 'Tom' and surname <> 'Hanks'
我有一个工作查询,returns 汤姆·汉克斯的所有合作演员(基本上是 朋友的朋友 问题)。
SELECT COUNT(name) as name FROM (
SELECT expand(out('ACTS_IN').in('ACTS_IN')) FROM Actor WHERE name = 'Tom Hanks'
) WHERE name <> 'Tom Hanks'
现在我正在尝试构建一个查询,该查询会为我提供合作演员的合作演员(朋友的朋友的朋友)
我的查询,未完成
SELECT count(DISTINCT(name)) as coactor FROM (
SELECT expand(out('ACTS_IN').in('ACTS_IN').out('ACTS_IN').in('ACTS_IN').out('ACTS_IN').in('ACTS_IN')) FROM Person WHERE name = 'Tom Hanks'
)
WHERE name <> 'Tom Hanks'
问题是,你能不能像这样堆叠 .in 和 .out:
expand(out('ACTS_IN').in('ACTS_IN').out('ACTS_IN').in('ACTS_IN').out('ACTS_IN').in('ACTS_IN'))
在您的情况下,您可以使用 TRAVERSE()
函数来检索边 ACTS_IN
.
结构:
create class Person extends V
create class Movie extends V
create class acts_In extends E
create class directed extends E
create class friend extends E
create class rated extends E
create property Person.name String
create property Person.surname String
create property Movie.title String
create vertex Person set name="Tom", surname="Hanks"
create vertex Person set name="Robin", surname="Wright"
create vertex Person set name="Helen", surname="Hunt"
create vertex Person set name="Robert", surname="Zemeckis"
create vertex Person set name="Russell", surname="Crowe"
create vertex Person set name="Ben", surname="Affleck"
create vertex Person set name="Kevin", surname="Macdonald"
create vertex Person set name="John"
create vertex Person set name="Mark"
create vertex Person set name="Paul"
create vertex Person set name="Mel", surname="Gibson"
create vertex Person set name="Nancy", surname="Meyers"
create vertex Movie set title="Forrest Gump"
create vertex Movie set title="Cast Away"
create vertex Movie set title="State of Play"
create vertex Movie set title="What Women Want"
create edge acts_In from (select from Person where name="Tom" and surname="Hanks") to (select from Movie where title="Forrest Gump")
create edge acts_In from (select from Person where name="Tom" and surname="Hanks") to (select from Movie where title="Cast Away")
create edge acts_In from (select from Person where name="Robin" and surname="Wright") to (select from Movie where title="Forrest Gump")
create edge acts_In from (select from Person where name="Robin" and surname="Wright") to (select from Movie where title="State of Play")
create edge acts_In from (select from Person where name="Helen" and surname="Hunt") to (select from Movie where title="Cast Away")
create edge acts_In from (select from Person where name="Helen" and surname="Hunt") to (select from Movie where title="What Women Want")
create edge acts_In from (select from Person where name="Mel" and surname="Gibson") to (select from Movie where title="What Women Want")
create edge acts_In from (select from Person where name="Russell" and surname="Crowe") to (select from Movie where title="State of Play")
create edge acts_In from (select from Person where name="Ben" and surname="Affleck") to (select from Movie where title="State of Play")
create edge friend from (select from Person where name="Mel" and surname="Gibson") to (select from Person where name="Helen" and surname="Hunt")
create edge friend from (select from Person where name="Ben" and surname="Affleck") to (select from Person where name="Russell" and surname="Crowe")
create edge directed from (select from Movie where title="What Women Want") to (select from Person where name="Nancy" and surname="Meyers")
create edge directed from (select from Movie where title="Cast Away") to (select from Person where name="Robert" and surname="Zemeckis")
create edge directed from (select from Movie where title="Forrest Gump") to (select from Person where name="Robert" and surname="Zemeckis")
create edge directed from (select from Movie where title="State of Play") to (select from Person where name="Kevin" and surname="Macdonald")
create edge rated from (select from Movie where title="What Women Want") to (select from Person where name="Paul")
create edge rated from (select from Movie where title="Cast Away") to (select from Person where name="John")
create edge rated from (select from Movie where title="Forrest Gump") to (select from Person where name="Mark")
create edge rated from (select from Movie where title="State of Play") to (select from Person where name="John")
查询 1:检索 'Tom Hanks' 的所有 actors/co-actors/ecc...(除了他自己)
SELECT expand(DISTINCT) FROM (SELECT DISTINCT(@rid) FROM (TRAVERSE BOTH('ACTS_IN')
FROM (SELECT FROM Person WHERE name = 'Tom' AND surname = 'Hanks') MAXDEPTH 4)
WHERE name <> 'Tom' and surname <> 'Hanks')
查询 2:检索 'Tom Hanks' 的 actors/co-actors/ecc... 的计数(除了他自己)
SELECT COUNT(DISTINCT(@rid)) AS co_actors FROM (TRAVERSE BOTH('ACTS_IN')
FROM (SELECT FROM Person WHERE name = 'Tom' AND surname = 'Hanks') MAXDEPTH 4)
WHERE name <> 'Tom' and surname <> 'Hanks'