I am trying to get all folders and consequent subfolders given multiple parent_ids. The database table has the following structure:
id name parent_id
1 Folder1 null
2 Folder2 null
3 Folder3 1
4 Folder4 1
5 Folder5 2
6 Folder6 2
7 Folder7 4
The following query works for a single id, however I want to avoid doing multiple queries. For multiple ids for instance 1 and 6, it should return the rows (1, 3, 4, 7 and 6).
SELECT id,
name,
parent_id
FROM
(SELECT *
FROM folder_categories
ORDER BY parent_id, id) categories_sorted,
(SELECT @pv := ?) initialisation
WHERE (FIND_IN_SET(parent_id, @pv)
AND @pv := CONCAT(@pv, ',', id))
OR id = ?
Note: Question mark might be any id.
Any idea how can I adapt this query to accept multiples ids?
Edit: My mysql version is 5.7 so unfortunately WITH RECURSIVE is not available.