I have following MySQL db table filled with data:
|id|parentId|position
|20|NULL |1
|21|NULL |2
|22|NULL |3
|23|21 |1
|24|21 |2
As you may notice, position inside parent starts from 1 as well.
So if I query table:
SELECT * FROM myTable ORDER BY IFNULL(parentId, id), parentId, position
I get following results:
|id|parentId|position
|20|NULL |1
|21|NULL |2
|23|21 |1
|24|21 |2
|22|NULL |3
What is actually fine, until I change the row(id#21) position to value 3,and row#22 position to value 2. Now I would like to expect following results:
|id|parentId|position
|20|NULL |1
|22|NULL |2
|21|NULL |3
|23|21 |1
|24|21 |2
Unfortunately nothing changes and I have the same order:
|id|parentId|position
|20|NULL |1
|21|NULL |2
|23|21 |1
|24|21 |2
|22|NULL |3
So any ideas how to change query to get wanted results?