I’m trying to get the first category (level zero, no parents) from a child category (which might be any level) with just one query with MySQL. Is that possible?
Here’s an example DB structure :
Table categories
id (INT 11)
parent (INT 11, zero if it has no parent)
name
edit: Would MPTT be a easier solution to the problem?
It is impossible to scan infinite levels with only one query but if you are sure your categoies tree doesn’t go deeper than a certain number of levels you can use this trick, this is for 4 levels: