--总数指记录数还是mm字段? 如果是记录数可用下面的sql
select * from a where where xb in (
select a.xb from
(select xb,count(*) f_cnt from a group by xb ) a1 ,
(select xb,count(*) f_cnt from b group by xb ) b1
where a1.xh = b1.xb and a1.f_cnt = b1.f_cnt )
----创建测试数据
declare @ta table(id int,xb int,mm int)
insert @ta
select 1, 11, 111 union all
select 2, 22, 222 union all
select 3, 33, 333 union all
select 4, 44, 444
declare @tb table(id int,xb int,mm int)
insert @tb
select 1, 11, 111 union all
select 2, 11, 112 union all
select 3, 22, 222 union all
select 4, 22, 221 union all
select 5, 33, 333 union all
select 6, 44, 444 union all
select 7, 55, 555
----查询
select * from @ta as a where not exists(select 1 from @tb where xb = a.xb and mm <> a.mm)
/*结果
id xb mm
----------- ----------- -----------
3 33 333
4 44 444
*/
select t1.* from t1
join
(
select xb,count(*)as i
from t2
group by xb
having count(*)=(select count(*) from t1 where t1.xb=t2.xb group by xb)
)a on t1.xb=a.xb
id xb mm
----------- ----------- -----------
3 33 333
4 44 444
5 66 1589
6 66 1478
----创建测试数据
declare @ta table(id int,xb int,mm int)
insert @ta
select 1, 11, 111 union all
select 2, 22, 222 union all
select 3, 33, 333 union all
select 4, 44, 444
declare @tb table(id int,xb int,mm int)
insert @tb
select 1, 11, 111 union all
select 2, 11, 112 union all
select 3, 22, 222 union all
select 4, 22, 221 union all
select 5, 33, 333 union all
select 6, 44, 444 union all
select 7, 55, 555
----查询
select xb from @ta as a group by xb having count(*) = (select count(*) from @tb where xb = a.xb)
create table t1
(id int, xb int, mm int)
insert into t1
select 1, 11, 111 union all
select 2, 22, 222 union all
select 3, 33, 333 union all
select 4, 44, 444
create table t2
(id int, xb int , mm int )
insert into t2
select 1, 11, 111 union all
select 2, 11, 112 union all
select 3, 22, 222 union all
select 4, 22, 221 union all
select 5, 33, 333 union all
select 6, 44, 444 union all
select 7, 55, 555
select t1.* from t1
join
(
select xb,count(*)as i from t2 group by xb having count(*)=1
)a on t1.xb=a.xb
id xb mm
----------- ----------- -----------
3 33 333
4 44 444