Two-column order

Let's say I have a table that looks like this:

enter image description here

As you can see, line with identifier 6 is the answer to line with identifier 3. I would like to order a result set so that it appears between lines 3 and 7. How to do this? Fulfillment: select * from comments_comment order by updated_at, response_to_idgets exactly what you can see in the image.

+4
source share
1 answer
ORDER BY COALESCE(response_to_id, id), id

The logic of your order is that you want the parent records and their children to appear together and the parent to appear on top of the group.

ORDER BY , , response_to_id id , response_to_id null. id.

+4

Source: https://habr.com/ru/post/1649988/


All Articles