On Feb 9, 1:29 am, Lew <l...@nospam.lewscanon.com> wrote:
> "sh" <shailajabt...@gmail.com> wrote:
> >> i have to get the tree sturcture of that boss( child boss s immediate
> >> parentBoss and parentBoss's immediate parentBoss and soon........)
> >> inorder to get this result how to a write a query in mysql db
> Manish Pandit wrote:
> > Per my understanding, you are looking for self-joins. Google for
> > 'mysql self join' and you should be able to get some information along
> > these lines. I believe this is a pretty common problem statement
> > intended to be solved using self joins.
>
> That's right. You want something along the lines of
>
> ... FROM T T1 JOIN T T2 ON T1.boss = T2.person ...
>
> Perhaps the people in one of the database or MySQL newsgroups can help better
> than here in the Java world. They could further help with information on how
> to structure foreign and primary keys to support this, and what dangers may lurk.
>
> - Lew
I don't think it can be done in one SELECT query, unless you assume
the table to be sorted in such an order that bosses are come after
employees,
which is dangerous.
The following query gives you the boss of a specific employee
SET @employee := 'an_employee_id';
SELECT t1.id AS employee_id, @boss := t2.id AS boss_id
FROM employees AS t1 JOIN employees AS t2
ON t1.boss = t2.id
WHERE t1.id = @employee;
Now, if @employee <> @boss, you need to repeat the SELECT query
after you set
SET @employee := @boss;
else you're done.
This can be achieved by a script procedure in MySQL or it could be
left to API.
Otherwise, you can subscribe to the proper mailing list for ask such
questions at
http://lists.mysql.com/
Regards,
Faton Berisha