sql查询只符合某一个
PS:本人新手,望指导:
卡号 姓名 项目
001 小王 A
001 小王 B
001 小王 C
003 小黄 A
004 小名 B
005 小兰 A
005 小兰 B
006 小张 A
006 小张 C
007 小陈 A
要求: 查询出只做过项目A的卡号、姓名
卡号 姓名 项目
003 小黄 A
007 小陈 A
[解决办法]
--> 测试数据:[test]if object_id('[test]') is not null drop table [test]create table [test]([卡号] varchar(3),[姓名] varchar(4),[项目] varchar(1))insert [test]select '001','小王','A' union allselect '001','小王','B' union allselect '001','小王','C' union allselect '003','小黄','A' union allselect '004','小名','B' union allselect '005','小兰','A' union allselect '005','小兰','B' union allselect '006','小张','A' union allselect '006','小张','C' union allselect '007','小陈','A'select * from test awhere not exists(select 1 from test b where a.姓名=b.姓名 and b.项目<>'A')and a.项目='A'/*卡号 姓名 项目003 小黄 A007 小陈 A*/