Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The simple approach would be to do some kind of simple nested clause. So say you have a table with posts and the posters id, and a friends table, the first layer would be</p> <pre><code>SELECT post FROM posts JOIN friends on post.userid = friends.friend_id WHERE friend.id = 1 (user ID) </code></pre> <p>then to get a friends of friends </p> <pre><code>SELECT post FROM posts JOIN (SELECT DISTINCT friends_2.friend_id FROM friends AS friends_1 JOIN friends as friends_2 on friends_1.friend_id = friends_2.id where friends_1.id = 1) AS friends wHERE post.userid = friends.friend_id AND mainid = 1 (user ID) </code></pre> <p>You can repeat this nesting each time you want to add another layer of friend abstraction. The problem with this approach is that it would take a very long time to execute. For every time you add a layer of friend abstraction you are increasing the complexity by a power of n (where n is the number of rows in your table). </p> <p>It is more likely that they are saving the viewable friends in a table somewhere, so lets make a new tabled called friends_web</p> <pre><code>user_id, friend_id, level </code></pre> <p>when a user friends someone, it adds that new friend into friends_web at a level of 0(since that friend is no people away) then adds that friends friends at a level of 1 (since its 1 friend away). In order to keep the table integrity you would also want to add the inverted record. To clarify if A adds B as a friend and C is a friend of B, the following two records would get added to our new table</p> <pre><code>A, C, 1 C, A, 1 </code></pre> <p>since now A can see C and C can see A. </p> <p>now when we want a query we just do</p> <pre><code> SELECT post FROM posts JOIN friends_web ON post.user_id = friends_web.friend_id WHERE friends_web.user_id = user_id AND friends_web.level &lt; 2 (or however deep you want to look) </code></pre> <p>by doing that you minimized your query complexity when doing post lookups while being able to look more then 1 layer deep into a friend web. </p> <p>Sorry for the long winded response.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload