I need to get the initial state as well as the latest state from a MySQL database. This is over two tables:
customer
| id | name | surname | dob | telephone | |
|---|---|---|---|---|---|
| 10 | Steve | Bobbly | 01-01-1970 | uvw@xyz.com | 0123456789 |
| 15 | James | Bond | 01-01-1950 | 007@bond.com | 0101010999 |
audit_log
| id | entity_id | property | old_value | new_value |
|---|---|---|---|---|
| 1 | 10 | name | John | Steve |
| 2 | 10 | abc@def.com | uvw@xyz.com | |
| 3 | 10 | telephone | 0123456789 |
What I expect is output like this:
| id | name | surname | dob | telephone | |
|---|---|---|---|---|---|
| 10 | Steve | Bobbly | 01-01-1970 | uvw@xyz.com | 0123456789 |
| 10_1 | John | Bobbly | 01-01-1970 | abc@def.com | |
| 15 | James | Bond | 01-01-1950 | 007@bond.com | 0101010999 |
I initially had a PHP script that runs through all the customer rows, and then matches them to the audit_log rows and generate output from there, but the speed is EXTREMELY slow and resource intensive.
Would something like this be possible directly in MySQL, and how would I do it?
EDIT
I've added additional rows to the customer and the output tables. The output table needs to contain all rows in customer, as well as a copy of the initial row, built from audit_log.