Recursive query in Oracle

后端 未结 2 672
傲寒
傲寒 2020-12-11 08:33

I am kind of new to the more advanced topics of PLSQL, so hopefully someone can help me out.

The problem: I have a table with messages sent between

2条回答
  •  生来不讨喜
    2020-12-11 09:33

    In Oracle this is easily done using CONNECT BY

    select message_id, parent_id, message_content
    from messages
    start with message_id = 97 -- this is the root of your conversation
    connect by prior message_id = parent_id;
    

    This walks the tree from top to bottom.

    If you want to walk the tree from a single message to the root, change the start with and the connect by part:

    select message_id, parent_id, message_content
    from messages
    start with message_id = 100 -- this is the root of your conversation
    connect by prior parent_id = message_id; -- this now goes "up" in the tree
    

提交回复
热议问题