Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is gonna be long :(</p> <p>Note that I didn't use this approach in the end because it's performance was worse the my original attempt using GROUP BY</p> <p>I had to modify the data table design for the PAGES table to include a new column to hold the id of the subsection that the page belonged to, so now the PAGES table has columns that indicate the section it belongs to, and the subsection also. That structure modification was only for testing and I did not use it in the final version.</p> <p>Here is the query I created using the concept of a UNION between 2 queries.</p> <pre><code>SELECT * FROM pages AS p JOIN -- create derived table of sections and subsections ( -- separate query to get sections (parent id = 0 ) SELECT s.id AS page_sec_id, s.id AS sec_id, s.name AS sec_name, NULL AS subsec_id, NULL AS subsec_name, s.parent_id AS parent_id FROM sections AS s WHERE s.parent_id = 0 UNION -- separate query to get subsection (parent id != 0) SELECT ss.id AS page_sec_id, ss.parent_id AS sec_id, -- need to get section name, so had to use weird subquery (SELECT name FROM sections WHERE parent_id =0 AND id = ss.parent_id) AS sec_name, ss.id AS subsec_id, ss.name AS subsec_name, ss.parent_id AS parent_id FROM sections AS ss WHERE ss.parent_id != 0 ) AS sss ON -- specify how PAGES table is joined to this derived table of sections and subsections -- pages linked to sections only ( p.section_id = sss.sec_id AND p.subsection_id = 0 AND sss.parent_id = 0) OR -- pages linked to subsections only ( p.section_id = sss.sec_id AND p.subsection_id = sss.subsec_id ) </code></pre> <p>This UNION query used <strong>0.0388 seconds</strong> for 5 rows of Pages and 4 rows of sections/subsections, versus the original query which used <strong>0.0017 seconds</strong>, so I stuck with the original as shown above in my question. BTW in my dev environment mysql is running on a P3 Katmai 450 Mhz 256 RAM to force me to write efficient queries :)</p> <p>Thanks for reading, if you have additional thoughts &amp; comments please add them.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload