Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A simple SQL Select query to crawl all connected people in a social graph?

What is the shortest or fastest SQL select query or SQL procedure to crawl a social graph. Imagine we have this table:

UId FriendId
1   2
2   1
2   4
1   3
5   7
7   5
7   8
5   9
9   7

We have two subset of people here, i'm talking about a sql query or procedure which if we pass:

Uid = 4 return the result set rows with uid : {1, 2, 3}

or if

Uid = 9 return the result set rows with uid : {5, 7, 8}

Sorry for my poor english.


1 Answers

So you want get all friends of someone, including n-th degree friends? I don't think it is possible without recursion.

How you can do that is explained here: https://inviqa.com/blog/graphs-database-sql-meets-social-network

like image 193
Caner Avatar answered Dec 07 '25 15:12

Caner