Hi,
I'm a bit of a database noob, so I was wondering if someone here could help. I can't post the actual problem I'm working on, but i hope the following hypothetical example will illustrate what I'm trying to do.
Take this table:
Code:
fromAccountNo | toAccountNo
4563 | 2276
2276 | 2666
2276 | 3798
2276 | 2100
2276 | 4563
2666 | 1249
...
It shows how money has moved through a number of accounts. E.g. cash has gone from #4563 to #2276 and then on to other accounts. Some has gone back again. Whilst most accounts will be connected in this way, there will also be accounts that are unconnected to most the others.
What I want to be able do is pick an account (say #4563) and list all the accounts that link to it and be able to track backwards from any account to #4563. I'd also like to be able state how many steps separate an arbitrary account from #4563, but I suppose I can work that out easily enough when I track backwards.
I'm currently using programming loops to do this (which requires many database queries), but I wondered if it is possible to do it in one SQL query and if I should do so.
Sorry if this isn't very clear, I'm not the best at explaining things. I'll try to clarify it more if anyone asks.