Skip to content
Advertisement

SQL Tree Query – WITH RECURSIVE & UNION. How to sort by a second value

I have a category table with a tree like structure (see image below). For a fast single query, I have the following SQL to get the full tree.

This query result in the following table

So the ordering according to path works well. But what I require is to have the order according to the column position, if the path-level is the same level (like id 2 & 4, and 3, 4, 5).

Hence the order of ID would want is

Epected data structure

How to I change my SQL statement to reflect that order?

Advertisement

Answer

It can be achieved this way https://www.db-fiddle.com/f/rpFiPjKSwHW88C4cp6o9Rm/0

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement