How to get history of mapping for following table data I have a history mapping table for UserId changes, where every time when UserId changes, a row for new UserId with old UserId inserted in the history table.
Below is the sample table and data:

UserIdNew | UserIdOld
---------------------
    5     |     1    
    10    |     5    
    15    |     10   


The above data explains that UserId 1 has gone with following transition from UserId 1 -> 5-> 10 -> 15.

I want to query all the Old Ids for a give UserIdNew, how can I do it in a single query?

For this case if UserIdNew = 15, then it should return 1,5,10
System Version Tables in MariaDB-10.3 offers a range of features associated with versioning. This provides easy history viewing however its hard to trace changes.

Any table structure that preserves a unique unmodified key as the starting ID will make all required queries easier rather than resorting to CTE, procedures or other required recursive implementations.