I have an database table set up as shown below:
copy_blocks
id (auto-increment int)
content (text)
parent_id (int)
When a piece of copy is inserted the first time, the parent_id is set to itself. Example:
copy_blocks
id content parent_id
1 Hello, World 1
When that copy is updated by the user, new rows are inserted, but those row always points to the first version of the copy block.
copy_blocks
id content parent_id
2 Hello, World! 1
3 Heya, World!! 1
Structuring things this way allows me to either query a specific version of the content, or find the parent copy block and then look up the latest version. (In this case, version #3)
So here’s my question: Can anyone come up with a query that will always return the latest version of every content block? Ideally the query would return the results below:
id content parent_id
3 Heya, World!! 1
I get the feeling it would have something to do with joining against itself. I can’t think of how to do it without at least two different queries or server-side code.
One more option: