Using an audit trail in MySQL, how would you find records that had a specific value during a certain time frame?
Let's say I want to get the ids of users who were a site admin one week ago (role_id=1).
Given the table:
**USER**
id | role_id | ...
And a revision table:
**TABLE_HISTORY**
table_name | date | id | column_name | column_value
(assume I'm storing create data in there for better or worse and I'm soft deleting)
How would you find people where role_id = 1 one week ago today using only one query? Keep in mind that their role might have changed to 2 after it was one, this means you can't just select where role_id = 1
Something like this maybe?:
SELECT * FROM
(
SELECT *
WHERE (date < $timestamp AND column_name = 'role_id')
LIMIT 1
)
WHERE ('is_admin'=1)