根据另一个表过滤具有无效字段值的数据

时间:2018-12-22 07:10:17

标签: sql sql-server sql-server-2014 sql-server-2016

请考虑以下脚本:

declare @FirstTable   Table
(
    Name            nvarchar(50), -- Key
    Gender          tinyint,      -- Key   1 for Male, 2 for Female
    DateModified    datetime
)


insert into @FirstTable
Values(N'Tom' , 1, GETDATE()),
      (N'Tom' , 2, GETDATE()),
      (N'Clare' , 2, GETDATE()),
      (N'Lauren' , 1, GETDATE()),
      (N'Sara' , 2, GETDATE()),
      (N'Al' , 1, GETDATE()),
      (N'Kevin' , 1, GETDATE()),
      (N'Meril' , 2, GETDATE())


declare @Movies   Table
(
    Id              int identity(1,1),      
    Name            nvarchar(50),           -- Key
    ActorName       Nvarchar(50),      
    ActorGender     tinyint,
    ReleaseDate     datetime
)


insert into @Movies
Values (N'Movie 1', N'Tom' , 1, GETDATE()),
       (N'Movie 2', N'Kevin' , 2, GETDATE()),
       (N'Movie 3', N'Meril' , 2, GETDATE()),
       (N'Movie 4', N'Meril' , 2, GETDATE()),
       (N'Movie 5', N'Clare' , 1, GETDATE()),
       (N'Movie 6', N'Tom' , 2, GETDATE()),
       (N'Movie 7', N'Al' , 1, GETDATE()),
       (N'Movie 8', N'Sara' , 1, GETDATE()),
       (N'Movie 9', N'Ben' , 2, GETDATE()),
       (N'Movie 10', N'Viera' , 2, GETDATE()),
       (N'Movie 11', N'George' , 2, GETDATE()),
       (N'Movie 12', N'Frank' , 2, GETDATE()),
       (N'Movie 13', N'Kevin' , 2, GETDATE())

select  Id,     
        tmp.Name, 
        ActorName,      
        ActorGender,
        ReleaseDate
from @Movies tmp inner join @FirstTable gn on tmp.ActorName = gn.Name
where tmp.ActorGender <> gn.Gender

我想在Movie表中选择性别无效的数据。例如,如果我们假设Tom是男性和女性之间的通用名称,则上面的查询会返回Tom的结果(而且不正确)。请帮助我改正我的选择声明。

编辑1) 有些记录(名称)仅存在于Movies表中,而FirstTable中没有对应记录。我不希望我的查询返回这些记录。

感谢@Nick,我想要这个结果:

Id  Name        ActorName   ActorGender     ReleaseDate
2   Movie 2     Kevin       2               22/12/2018 07:20:14
5   Movie 5     Clare       1               22/12/2018 07:20:14
8   Movie 8     Sara        1               22/12/2018 07:20:14
10  Movie 10    Kevin       2               22/12/2018 07:20:14

谢谢

1 个答案:

答案 0 :(得分:2)

更新

由于您不想获取Movies表中不存在的FirstTable表中名称的记录,因此我们可以使用{{1 }}在JOINMovies.ActorName上。然后,我们可以像以前一样使用FirstTable.Name来查找性别不匹配的演员:

LEFT JOIN

更新后的输出:

Select  Id,     
        tmp.Name, 
        ActorName,      
        ActorGender,
        ReleaseDate
from Movies tmp 
join FirstTable gn on tmp.ActorName = gn.Name
left join FirstTable gn2 on tmp.ActorName = gn2.Name AND tmp.ActorGender = gn2.Gender
where gn2.Name IS NULL

Updated demo on dbfiddle

原始答案

您可以使用Id Name ActorName ActorGender ReleaseDate 2 Movie 2 Kevin 2 22/12/2018 07:20:14 5 Movie 5 Clare 1 22/12/2018 07:20:14 8 Movie 8 Sara 1 22/12/2018 07:20:14 13 Movie 13 Kevin 2 22/12/2018 07:20:14 LEFT JOIN表中查找MoviesActorGender表中适当的Gender不匹配的条目:

FirstTable

输出:

Select  Id,     
        tmp.Name, 
        ActorName,      
        ActorGender,
        ReleaseDate
from Movies tmp 
left join FirstTable gn on tmp.ActorName = gn.Name AND tmp.ActorGender = gn.Gender
where gn.Name IS NULL

Demo on dbfiddle