I'm looking for the simplest way to recursively get all the parent elements from a database using the adjacency list / single table inheritance model (id, parent_id
).
My select currently looks like this:
$sql = "SELECT
e.id,
TIME_FORMAT(e.start_time, '%H:%i') AS start_time,
$title AS title,
$description AS description,
$type AS type,
$place_name AS place_name,
p.parent_id AS place_parent_id,
p.city AS place_city,
p.country AS place_country
FROM event AS e
LEFT JOIN place AS p ON p.id = e.place_id
LEFT JOIN event_type AS et ON et.id = e.event_type_id
WHERE e.day_id = '$day_id'
AND e.private_flag = 0
ORDER BY start_time";
Each event
is linked to a place
, and each place
can be a child of another place
(upto about 5 levels deep)
Is this possible in a single select with mysql?
At the moment I am thinking it could be a separate function which loops through the returned $events
array, adding place_parent_X
elements as it goes, but am not sure how to implement this.