0

I have one table with example data:

+----+---------+
| id | rede_id |
+----+---------+
|  1 |       0 |
|  2 |      38 |
|  3 |       1 |
| 38 |       1 |
| 40 |       1 |
| 41 |      38 |
| 42 |      38 |
| 43 |      40 |

rede_id means what id some person belongs to. Its a network system.

For example, if I need to check network of id=1, the results needs to be like:

+----+---------+
| id | rede_id |
+----+---------+
|  3 |       1 |
| 38 |       1 |
| 40 |       1 |
| 41 |      38 |
| 42 |      38 |
| 40 |       1 |

And if rede_id of someone is '41' or '42' needs to be on results to. Goes to infinite. I can have N rede_id with my id, N rede_id with some id that belongs to me and infinite... I need to get all results.

I don't know how to do that... Sincerely no I ideia.

1
  • Where's you query????? Commented Mar 8, 2018 at 17:55

1 Answer 1

1

MySQL 8.0 now supports recursive queries, documented here: https://dev.mysql.com/doc/refman/8.0/en/with.html#common-table-expressions-recursive

Before MySQL 8.0, there's no easy solution for querying this type of data.

There are alternative ways of storing the data, to make it easier to query.

See also:

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.