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

SQL query check value exists in lookup based on another column value

我在 table 中有 Name/Value 对记录,我需要根据每个名称

的查找确认这些值是否存在

KeyVal - Table 个 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 - Table 匹配名称值(来自 KeyVal)和 LookupValue(在 CheckVals table)

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

CheckVals - Table 每个字段的有效值

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

我有一个查询将根据单个名称查找检查值,但我不确定如何根据查找检查所有名称 table。在此查询中,它绕过了 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 table 中的 所有 值进行查找,通过LookupTable table,检查 MyFieldValue 中的值是否存在于 CheckVals 中是否与 MyFieldNameLookupName 匹配?

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

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

样本Tables

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')

假设您想要第一个 table 中值不匹配的行:

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;