mysqlsqlperformancerelational-databaserelationships

Multiple relations parent/child with multiple levels


I have a MySQL table named companies like this:

+---------+-----------+-----------+
| id_comp | comp_name | id_parent |
+---------+-----------+-----------+
|       1 | comp1     |      NULL |
|       2 | comp2     |         1 |
|       3 | comp3     |         2 |
|       4 | comp4     |         2 |
|       5 | comp5     |         2 |
|       6 | comp6     |         1 |
|       3 | comp3     |         6 |
|       5 | comp5     |         6 |
|       7 | comp7     |         6 |
|       4 | comp4     |         6 |
|       8 | comp8     |         4 |
+---------+-----------+-----------+

Each company may have multiple parents (ex: comp3, which is child of comp2 and comp6), each parent may have multiple childs and each child can be a parent itself of multiple childs and so on... So, it can have unlimited levels (relations).

I researched several solutions (http://www.codeproject.com/Articles/818694/SQL-queries-to-manage-hierarchical-or-parent-child, http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/), but I don't think it fits my the problem since the same company (based on id_comp column) can have multiple parents.

I have two questions regarding this:

  1. Is this the right approach if I have thousands of relations (scalable) ?
  2. How do I, given a name (which is unique, based on id_comp) query to select its brothers (same parent_id), its direct parent(s), and its direct child(s).

Solution

  • Mysql isn't the best choice if you need to work with hierarchical data (getting all ancestors/descendants can be tricky). But if all you care about is finding direct parents/children, your table should be fine (although I might break it out into separate Company and CompanyParent tables so that the company name isn't entered multiple times).

    This would give you brothers:

    select name
    from companies 
    where id_parent in (select id_parent from companies where id_comp = @company_id)
    and id_comp <> @company_id
    group by name;
    

    This would give you direct parents:

    select p.name
    from companies p
    join companies c on p.id = c.id_parent
    where c.id_comp = @company_id
    group by c.name;
    

    This would give you direct children:

    select c.name
    from companies p
    join companies c on p.id = c.id_parent
    where p.id_comp = @company_id
    group by c.name;