cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Create a query that brings data from two different nodes that doesn't have any relationship

Hello guys, it’s me again.

Now, I would like to make a select “match” with two situations. I am mean, with two different nodes. The bellow query give me my answer, but I need to execute two separate query. Instead of that I need to execute only one query to return both answers. Is it possible?

match (pu:PessoaUnica {CPF:'00000000001'}) return pu;

match (po:PessoaOrigem {CPF:'00000000001'}) return po;

Before asking this help, I try to find the answers on the internet and the closer that I found was using union all, but to use it the return must be equal so it doesn’t work for my business.

Regards,

Igor Bastos Martins

1 ACCEPTED SOLUTION

Hi Rogerio, I solve this problem putting an alias like:

match ( pu:PessoaUnica {CPF:'00000000001'}) return pu as po
union all
match ( po:PessoaOrigem {CPF:'00000000001'}) return po

Thanks

View solution in original post

5 REPLIES 5

@igorbmartins

UNION combines the results of two or more queries into a single result set that includes 
all the rows that belong to all queries in the union.

The number and the names of the columns must be identical in all queries 
combined by using UNION.

Hello dana.canzano, the problem to use UNION ALL it is because they have a premiss that all columns must be identical in both query. In my case it is not true. In the first query I want as result the node X and in the second query the result is Y.
Do you know some way to do that?

@igorbmartins

the requirement is the name and number of columns returned is the same. You can however rename as column using the as clause , i.e match (n:Person) return n.name as fullname; for the query will rename the returning column n.name to fullname

Maybe you can use the WITH clause, something like this:

MATCH (n)-[]-(m) WHERE X
WITH n
MATCH (a)-[]-(b) WHERE Y
WITH n,a
RETURN n.name, a.age

Hi Rogerio, I solve this problem putting an alias like:

match ( pu:PessoaUnica {CPF:'00000000001'}) return pu as po
union all
match ( po:PessoaOrigem {CPF:'00000000001'}) return po

Thanks