2

Say I have a data base with classes and requirements that looks like this for example:

+--------+------+
| course |prereq|
+--------+------+
| 1      |  null|
| 2      |  null|
| 3      |    1 |
| 3      |    2 |
| 4      |    3 |
| 5      |    4 |
+--------+------+

And I want to find all the prereqs for course 4. So my answer would be 3,2,1.
Also this is just an example I want to be able to use this for a table of any size.

How would I go about doing this in mysql?

Praveen Prasannan
  • 7,093
  • 10
  • 50
  • 70
Katseye
  • 63
  • 5

1 Answers1

2

What you are asking is about hierarchical joins in mysql.

With a sort of logic and a stored function will do the trick for you.

This thread surely will lead you.

Have a try.

Community
  • 1
  • 1
Praveen Prasannan
  • 7,093
  • 10
  • 50
  • 70
  • 1
    +1 I love the link to a google search! Makes the OP do some research, but sometimes knowing what to search for is the tricky part – amaster Jul 30 '13 at 04:03