A simplified example:
I have a SQL table called things
. Things by themselves have an id
and a name
. Things are part of a tree, e.g. a thing can have a parent; Exactly how to this is stored is not important, important is however that it is possible to obtain a list of thing ids from the root node to the current thing.
I have another table, called properties
. A property has a thing_id
column, a name
column and a value
column.
I now want, for the current thing, to obtain all properties, ordered by thing_id, in order of the paths from root thing to current thing.
e.g., if the current thing is nested like this: Root(1) > Vehicle(4) > Car(2) > Hybrid(3)
, I would want the list of properties be returned with the properties that have a thing_id==1
first, followed by the ones with thing_id == 4
, then thing_id==2
and finally thing_id==3
.
How can this be done using SQL? (without using N+1 selects)