SQL查询检查值存在于基于另一列值的查找中

时间:2016-09-13 21:56:58

标签: sql sql-server lookup-tables

我在表中有名称/值对记录,我需要确认每个名称的查找存在的值

KeyVal - NameValue对表

| MyID1 | MyRecNumber | MyFieldName | MyFieldValue |
|-------|-------------|-------------|--------------|
| 1     | 1           | FirstField  | One          |
| 2     | 1           | SecondField | Car          |
| 3     | 2           | FirstField  | Two          |
| 4     | 2           | SecondField | Firetruck    |
| 5     | 3           | FirstField  | Blue         |
| 6     | 3           | SecondField | Car          |

LookupTable - 将名称值(来自KeyVal)与LookupValue匹配的表(在CheckVals表中)

| MyID2 | MyFieldName | LookupName |
|-------|-------------|------------|
| 1     | FirstField  | FieldOne   |
| 2     | SecondField | FieldTwo   |

CheckVals - 每个字段的有效值

| MyID3 | LookupFieldName | LookupValue |
|-------|-----------------|-------------|
| 1     | FieldOne        | One         |
| 2     | FieldOne        | Two         |
| 3     | FieldOne        | Three       |
| 4     | FieldTwo        | Car         |
| 5     | FieldTwo        | Truck       |
| 6     | FieldTwo        | Bus         |

我有一个查询将针对单个名称查找检查值,但我不确定如何根据查找表检查所有名称。在此查询中,当我在查询本身中指定查找值时,它会绕过LookupTable

DECLARE @AttributeName AS  VARCHAR(50)
DECLARE @Lookup AS  VARCHAR(50)

SET @AttributeName = 'SecondField'
SET @Lookup = 'FieldTwo';

SELECT
    MyRecNumber,
    MyFieldName,
    MyFieldValue
FROM
    dbo.KeyVal kv
WHERE
    MyFieldName = @AttributeName
    AND MyFieldValue NOT IN
        (
            SELECT
                LookupValue
            FROM
                dbo.CheckVals cv
            WHERE cv.LookupFieldName = @Lookup
        )

问题:如何通过KeyVal表格对LookupTable表中的所有值进行查找,以检查MyFieldValue中的CheckVals中的值与MyFieldNameLookupName匹配相对应?

这是我希望得到的 - 在查询结果中返回具有无效值的两行

| MyRecNumber | MyFieldName | MyFieldValue |
|-------------|-------------|--------------|
| 2           | SecondField | Firetruck    |
| 3           | FirstField  | Blue         |

样本表

CREATE TABLE [dbo].[KeyVal](
    [MyID1] [smallint] IDENTITY(1,1) NOT NULL,
    [MyRecNumber] [smallint] NULL,
    [MyFieldName] [varchar](50) NULL,
    [MyFieldValue] [varchar](50) NULL
) ON [PRIMARY]

CREATE TABLE [dbo].[LookupTable](
    [MyID2] [smallint] IDENTITY(1,1) NOT NULL,
    [MyFieldName] [varchar](50) NULL,
    [LookupName] [varchar](50) NULL
) ON [PRIMARY]

CREATE TABLE [dbo].[CheckVals](
    [MyID3] [smallint] IDENTITY(1,1) NOT NULL,
    [LookupFieldName] [varchar](50) NULL,
    [LookupValue] [varchar](50) NULL
) ON [PRIMARY]

样本数据

INSERT INTO [dbo].[KeyVal]
    ([MyRecNumber], [MyFieldName], [MyFieldValue])
VALUES
    (1, 'FirstField', 'One'),
    (1, 'SecondField', 'Car'),
    (2, 'FirstField', 'Two'),
    (2, 'SecondField', 'Firetruck'),
    (3, 'FirstField', 'Blue'),
    (3, 'SecondField', 'Car')

INSERT INTO [dbo].[LookupTable]
    ([MyFieldName], [LookupName])
VALUES
    ('FirstField', 'FieldOne'),
    ('SecondField', 'FieldTwo')

INSERT INTO [dbo].[CheckVals]
    ([LookupFieldName], [LookupValue])
VALUES
    ('FieldOne', 'One'),
    ('FieldOne', 'Two'),
    ('FieldOne', 'Three'),
    ('FieldTwo', 'Car'),
    ('FieldTwo', 'Truck'),
    ('FieldTwo', 'Bus')

1 个答案:

答案 0 :(得分:1)

我假设你想要第一个表中值不匹配的行:

select kv.*
from keyval kv left join
     lookuptable lt
     on kv.myfieldname = lt.myfieldname left join
     checkvals cv
     on cv.LookupFieldName = lt.LookupName and
        cv.LookupValue = kv.MyFieldValue
where cv.myid3 is null;