What SQL query should I perform to get the result set expected?

Posted by texai on Stack Overflow See other posts from Stack Overflow or by texai
Published on 2010-12-16T23:39:23Z Indexed on 2010/12/28 22:53 UTC
Read the original article Hit count: 144

Filed under:
|
|

What SQL query should I perform to get the result set expected, giving the first element of the chain (2) as input data, or any of them ?

table name: changes
+----+---------------+---------------+
| id | new_record_id | old_record_id |
+----+---------------+---------------+
|   1|              4|              2|
|      -- non relevant data --       |
|   6|              7|              4|
|      -- non relevant data --       |
|  11|             13|              7|
|  12|             14|             13|
|      -- non relevant data --       |
|  31|             20|             14|
+----+---------------+---------------+


Result set expected:

+--+
| 2|
| 4|
| 7|
|13|
|14|
|20|
+--+

I know I should consider change my data model, but: What if I couldn't?

Thank you in advance!

© Stack Overflow or respective owner

Related posts about sql

Related posts about mysql