简体   繁体   English

SQL Select Text用于多个外键以查找同一行中的表

[英]SQL Select Text for multiple foreign keys to lookup table in same row

I have a table similar to the following that has a history of changes to an item and holds the old and new value for a status. 我有一个类似于以下表格,该表格具有更改项目的历史记录,并保存状态的新旧值。 The status number is a foreign key to a lookup table that holds the text. 状态号是保存文本的查找表的外键。 Ie 1 = 'In Inventory', 2= 'Destroyed' etc.. 即1 =“库存中”,2 =“销毁”等。

I want to be able to present this as human readable results and replace the integer keys with the text from the lookup table but I'm not quite sure how to do that as I can't just join on the 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 the status table multiple times - something like this: 您可以多次join status表-像这样:

select i.item_id, 
    i.old_status, 
    i.new_status,
    i.timestamp,
    s1.statustext,
    s2.statustext
from items i
    join status s1 on i.old_status = s1.statusid
    join status s2 on i.new_status = s2.statusid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM