【发布时间】:2026-01-04 20:50:02
【问题描述】:
我有一个关于 select 的棘手问题:我有这个表结构:
declare @one as table (serviceid int null)
declare @two as table (id int null, serviceid int null)
insert into @two
values (15,195),(15,84),(16,195),(16,84),(16,NULL),(17,195),(17,84),(17,8)
我需要得到完全匹配的 @two.ID(与 serviceid 和 count intable @two 匹配)
场景 1:
insert into @one values (195),(84)
我只需要获取 ID-15,因为所有 serviceid 都匹配并且表 @one 中的记录数为 2。
场景 2:
Insert into @one values (195),(84),(8)
我只需要获取 ID- 16 和 17,17:因为所有 serviceid 都匹配并且表 @one 中的记录数为 3。,16:因为两个服务匹配,表 @one 中的记录数为 3,NULL 表示“无关紧要”(谁)
你有什么想法吗?
【问题讨论】:
标签: sql sql-server select matching