join - SQL Select Text for multiple foreign keys to lookup table in same row -
i have table similar following has history of changes item , holds old , new value status. status number foreign key lookup table holds text. i.e. 1 = 'in inventory', 2= 'destroyed' etc..
i want able present human readable results , replace integer keys text lookup table i'm not quite sure how can't join on foreign key.
demo database
+---------+-------------+-------------+------------+ | item_id | old_status | new_status | timestamp | +---------+-------------+-------------+------------+ | 1 | 1 | 2 | 2012-03-25 | | 1 | 2 | 3 | 2013-12-25 | | 1 | 3 | 4 | 2015-03-25 | +---------+-------------+-------------+------------+
you can join
on status
table multiple times - this:
select i.item_id, i.old_status, i.new_status, i.timestamp, s1.statustext, s2.statustext items join status s1 on i.old_status = s1.statusid join status s2 on i.new_status = s2.statusid
Comments
Post a Comment