Ad

How To Fetch Data From One Class To Another In Orientdb

- 1 answer

I have created two classes customer and city. customer class contain two properties name and location and city class contain id and location . I want to perform join operations on these two classes. I create a graph relation in orientdb studio and fire a query below

select from customer where city.location='pune'

but this query not returning any value,it executed but not returning any field, So, this is correct syntax or i am doing wrong in somewhere .. please give me solutions.

Ad

Answer

I have this simple dataset to give you some examples:

create class Customer extends V
create class City extends V
create class livesAt extends E

create property Customer.name String
create property City.id integer
create property City.location String

create vertex Customer set name="Tom"
create vertex Customer set name="John"
create vertex City set id=1, location="London"
create vertex City set id=2, location="Pune"

create edge livesAt from (select from Customer where name="Tom") to (select from City where id=1)
create edge livesAt from (select from Customer where name="John") to (select from City where id=2)

Now you can use different queries to retrieve the results you're looking for.


Query 1a: Starting from Customer (like your query above)

select from Customer where out('livesAt').location in 'Pune'

Output:

----+-----+--------+----+-----------
#   |@RID |@CLASS  |name|out_livesAt
----+-----+--------+----+-----------
0   |#12:1|Customer|John|[size=1]
----+-----+--------+----+-----------

Query 1b: Starting again from Customer

select from Customer where out('livesAt').location contains 'Pune'

Output:

----+-----+--------+----+-----------
#   |@RID |@CLASS  |name|out_livesAt
----+-----+--------+----+-----------
0   |#12:1|Customer|John|[size=1]
----+-----+--------+----+-----------

Query 1c:

select from Customer where out('livesAt')[location = 'Pune'].size() > 0

Output:

----+-----+--------+----+-----------
#   |@RID |@CLASS  |name|out_livesAt
----+-----+--------+----+-----------
0   |#12:1|Customer|John|[size=1]
----+-----+--------+----+-----------

Query 2: Starting from City (more direct)

select expand(in('livesAt')) from City where location = 'Pune'

Output:

----+-----+--------+----+-----------
#   |@RID |@CLASS  |name|out_livesAt
----+-----+--------+----+-----------
0   |#12:1|Customer|John|[size=1]
----+-----+--------+----+-----------

Hope it helps

Ad
source: stackoverflow.com
Ad