表如图所示,用sql语言,把code字段只有CD的id找出来 结果 a005,a008
表名test2 字段id code
a009只含有C
a010 只有D

表如图所示,用sql语言,把code字段只有CD的id找出来 结果 a005,a008
表名test2 字段id code
a009只含有C
a010 只有D

select distinct id from Test1
where id in (select id from (select id,count(*) as cnt from Test1 where code = 'C' or code = 'D'
group by id) as t
where cnt=2)
and id not in(select id as times from Test1 where code != 'C' and code != 'D'
group by id)
取出code 同时有 cd的id ①
取出code 不同时含有cd的id ②
然后 id在①不在②的就是你要的 简单测试了一下好像可以 你再试试吧