Victor Nascimento
Victor Nascimento

Reputation: 781

How to get only incoming edges on mySQL graph?

I have a Graph represented on a mySQL table as:

uidFrom, uidTo
   1       4
   4       1
   1       5
   5       1
   6       1  <- only incoming edge

How to get only the "6, 1" pair, having only "1" as input? I mean, how to query the only incoming edges to node "1" (not both incoming and outgoing like 4 or 5)?

Thanks in advance;

Upvotes: 2

Views: 62

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726909

You can do it like this:

SELECT uidFrom, uidTo
FROM Graph g
WHERE NOT EXISTS (
    SELECT * FROM Graph rev WHERE rev.uidFrom=g.uidTo AND rev.uidTo=g.uidFrom
)

Here is a demo on sqlfiddle.

Upvotes: 1

Related Questions