I feel like I'm over complicating this, but for the life of me I can't seem to make this work without looping through queries and I'd like to accomplish this in a single query if possible. The replaced\_by creates a chain. One record can be replaced by another record and by another record, etc. I want to retrieve the last replacement when generally querying

```

id    type    color    replaced_by
1     color   red      null
2     color   blue     3
3     color   teal     4
4     color   aqua     null
5     fruit   orange   null

```

Given the above table, I'd like to...

do a `select id, type, color` where I get the following returned

```

1     color   red
4     color   aqua
5     fruit   orange

```

do a `select id, type, color where type="color"`I get the following returned

```

1     color   red
4     color   aqua

```

or even a `select color where id="2"` and get the following returned

```

aqua

```