选择QUERY内部连接字段以获取文本值而不是ID' s

时间:2016-11-10 20:15:16

标签: sql sql-server tsql select sql-server-2012

我有一张看起来像这样的桌子......

2016-11-10T19:58:29.4167668Z npm ERR! enoent ENOENT: no such file or directory, open 'C:\a\1\s\package.json'
2016-11-10T19:58:29.4177672Z npm ERR! enoent ENOENT: no such file or directory, open 'C:\a\1\s\package.json'
2016-11-10T19:58:29.4177672Z npm ERR! enoent This is most likely not a problem with npm itself
2016-11-10T19:58:29.4177672Z npm ERR! enoent and is related to npm not being able to find a file.
2016-11-10T19:58:29.4177672Z npm ERR! enoent 
2016-11-10T19:58:29.4297670Z 
2016-11-10T19:58:29.4297670Z npm ERR! Please include the following file with any support request:
2016-11-10T19:58:29.4297670Z npm ERR!     C:\a\1\s\npm-debug.log
2016-11-10T19:58:29.4457679Z ##[error]Npm failed with error: C:\Program Files\nodejs\npm.cmd failed with return code: 4294963238

这是表格的样子。如果用户编辑了记录,我保存了原始值和新值。现在,这就是我想要做的事情。我需要能够显示文本而不是ID(数字)。除了(在这个例子中)AmountPaid和Address之外 - 所有值都在tblDefinition中,看起来像这样......

Tbl1
User           OriginalValue   NewValue            FieldName
MikeS          11              12                  Dept
BobL           140             141                 Position
JohnS          11 South St     13 South St         Address
JenB           9               12                  AmountPaid
BillyS         133             132                 Seller

所以我想包含一堆数据来说明tblDefinition的样子。我尝试以某种方式能够使用tbl1中的Field和ID,并使用tblDefinition将其加入以获取文本值。我需要使用FieldName,因为可能出现在原始值和新值中的某些值(如Fieldname = AmountPaid)在tblDefinition中具有相应的值,但实际上我需要按原样显示值 - 在本例中为9和12

tblDefinition
ID              Name             Field
9               SomeValue        SomeX
10              SomeValue        SomeX
11              Accounting       Dept
12              Finance          Dept
132             Microsoft        Seller
133             Apple            Seller
140             Manager          Position
141             Entry            Position

这是我一直在尝试的但是这不会发生在任何地方 - 我真的很难弄清楚我是否可以在一个查询中做出类似的事情,或者我是否需要为tblDefinitions中具有相应文本值或

的每个字段执行多个联合

EDIT。也许就像....

Select OriginalValue,  Newvalue, Field
from tblDefinitions
LEFT JOIN tblDefinitions on (tbl2.OriginalValue = tblDefinitions.ID and tbl2.NewValue=tblDefinitions.ID)

也许只要我只指定要查找的字段,它就不会影响其他值,例如tbl1中的AMOUNT PAID

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您应该只加入定义表两次,一次是原始名称,一次是新名称。

SELECT tbl2.OriginalValue
    ,tdo.name as OriginalName
    ,tbl2.Newvalue
    ,tdn.name as NewName
    ,tbl2.user
FROM tblBHH_HXfieldChanges tbl2
LEFT JOIN tblDefinitions tdo
        on tbl2.OriginalValue = tdo.ID
left join tblDefinitions tdn 
        on tbl2.NewValue = tdn.ID