1

I have this hierarchical table that will end up containing 5 or 600 unique names, many of which will be assigned to multiple parents, such as "Hunt" below...

+-------------+----------------------+--------+
| category_id | name                 | parent |
+-------------+----------------------+--------+
|           1 | Phillips             |   NULL |
|           2 | Barnes               |      1 |
|           3 | Moore                |      2 |
|           4 | de Mohr              |      3 |
|           5 | Oswald               |      9 |
|           6 | Hunt                 |      2 |
|           7 | Burnham              |      6 |
|           8 | Buckley              |      7 |
|           9 | Paine                |      4 |
|          10 | Sturgis              |      6 |
|          11 | Hunt                 |      3 |
+-------------+----------------------+--------+

Query:

SELECT
  t1.name AS lev1,
  t2.name AS lev2,
  t3.name AS lev3,
  t4.name AS lev4,
  t5.name AS lev5,
  t6.name AS lev6
FROM category t1
  LEFT JOIN category t2
    ON t2.parent = t1.category_id
  LEFT JOIN category t3
    ON t3.parent = t2.category_id
  LEFT JOIN category t4
    ON t4.parent = t3.category_id
  LEFT JOIN category t5
    ON t5.parent = t4.category_id
  LEFT JOIN category t6
    ON t6.parent = t5.category_id
WHERE t1.name = 'Phillips'

Output:

+-------------+-------------+-------------+---------+---------+---------+
| lev1        | lev2        | lev3        | lev4    | lev5    | lev6    |
+-------------+-------------+-------------+---------+---------+---------+
| Phillips    | Barnes      | Hunt        | Sturgis | NULL    | NULL    |
| Phillips    | Barnes      | Moore       | de Mohr | Paine   | Oswald  |
| Phillips    | Barnes      | Hunt        | Burnham | Buckley | NULL    |
+-------------+-------------+-------------+---------+---------+---------+

I just have 2 questions about this.

Is it going to be wiser to incorporate a linking table to avoid duplicating "names" for each parent in one table, or is there some other way to avoid this, or is this even an issue?

The output I'll be getting for each page will be a long, fairly complex outline layout, which would probably mean that I'd need to run this kind of query sometimes up to 30 or 40 times per page. Is this kind of query something I can run that many times to load a page without a problem, or do I need to come up with something more efficient?

4
  • 2
    I don't know which RDBMS are you use but I it probably has some syntax to work with hierarchial query(Oracle and MSSQL can)? I don't understand why this query must be run 30 or 40 times per page. Just try to look at how to organize pagination.
    – Evgeniy K.
    Commented Sep 26, 2016 at 2:49
  • 1
    Why don't you use a recursive query?
    – user1822
    Commented Sep 26, 2016 at 7:16
  • I should have pointed out that I'm not a db programmer - I'm in WordPress, CSS and some PHP. Raw MySQL and db design is quite foreign to me. This is a singular pretty complex research project for myself; I don't foresee continuing on in the custom db arena. Having worked on getting my research into a db for a week, I only learned yesterday that what I'm trying to do is "hierarchical." I wouldn't know a "recursive" query if it bit me. What I'm saying is, basically I hardly know what questions to ask to find the answer I need... :)
    – glenn nall
    Commented Sep 26, 2016 at 10:14
  • I figured out how to incorporate a "linking table" last night, and that will work much better. I guess I really need some help in avoiding running the query multiple times, i.e. "WHERE t1.name = 'Phillips'" ...
    – glenn nall
    Commented Sep 26, 2016 at 10:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.