I have a table structure which looks like
id | parent | online | name
1 | 1 | 1 | first
2 | 1 | 1 | second
3 | 1 | 1 | third
Basically what I want is, if a column is set to offline then we don’t select that column,
easy enough
Select * from `table` where `online` = 1;
Next part of it becomes, if a columns parent is set to offline, we don’t want to select the column either.
My working is
SELECT t1.* FROM `table` t1 LEFT JOIN `table` t2 ON t1.`parent` = t2.`id`
WHERE t1.`online` = 1 AND t2.`online` = 1;
Which works fine if the nesting is at most one level deep.
Is there a better way of doing this if the nesting is only one level deep?
other point is, is there a better way of doing this so that the nesting can be an unlimited amount of levels deep?
If there is the structure would need to allow simple (both load wise and code wise) queries to get all children of a row, and to get all parents of a row with the nesting level relative to the child (e.g. first parent’s level would be 1, parent of parent would be 2).
I can suggest an answer the first part where the nesting is one-level deep:
I believe will be slightly more efficient, for the reason that you’re increasing the cardinality of the statement.
Unfortunately, multi-level nesting is a tricky subject, I’d do it on SQL Server using a CTE, but I don’t know about this in MySQL, sorry.