You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ORDER BY p.lft ASC' at line 1
SELECT p.id, p.pgname FROM pg_tree n, pg_tree p WHERE n.lft BETWEEN p.lft AND p.rgt AND n.id = ORDER BY p.lft ASC