OrientDB查询没有来自另一个顶点的特定边的顶点

时间:2015-04-23 17:34:12

标签: sql orientdb edge vertex

我有一个Post和一个User个顶点,与Posted边缘相关。

我想检索特定用户没有发布关系的帖子。

这是我的SQL查询:

SELECT posts.* FROM posts WHERE posts.user_id != 19.

我不能做像

这样的事情

SELECT FROM Post WHERE user.id != 19

因为我没有类之间的链接,所以我有优势。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

create class User extends V
create class Post extends V

create class Posted extends E

create vertex User set name = 'user1'     
create vertex User set name = 'user2'     

create vertex Post set name = 'post0'
create vertex Post set name = 'post1'
create vertex Post set name = 'post2'

create edge Posted from (select from Post where name = 'post0') to (select from User where name = 'user1')
create edge Posted from (select from Post where name = 'post1') to (select from User where name = 'user1')
create edge Posted from (select from Post where name = 'post2') to (select from User where name = 'user2')

如果您有类似上述内容,例如,您想知道与 user1 无关的所有帖子(即 post2 )。假设 user1 的记录ID为#12:0

你可以:

select from Post where  #12:0 not in out('Posted')
// or
select expand(in('Posted')) from (select from User where @rid not in [#12:0])

输出: enter image description here

<强>更新

您还可以使用用户名而不是 @rid

select * from Post where $record_id.rid[0] not in out('Posted')
let $record_id = ( select @rid from User where name = 'user1' )

但请注意, let 子句会针对每个帖子执行一次(在这种情况下,为3次)。这意味着当你有很多帖子时效率会不高。