日期:2014-05-18 浏览次数:20930 次
create table T( id int,name varchar(4)) insert into T values(1,'A'),(1,'B'),(2,'A'),(2,'C'),(3,'C'); --选出name有A无B的id号 --方法一: select * from (select * from T where name='A')as a where ID<>(select id from T where name='B')
create table T( id int,name varchar(4)) insert into T values(1,'A'),(1,'B'),(2,'A'),(2,'C'),(3,'C'); select t1.* from T t1 where t1.name='A' and not exists (select 1 from T t2 where t2.id=t1.id and t2.name='B') /* id name ----------- ---- 2 A (1 row(s) affected) */
------解决方案--------------------
SELECT  *
FROM    t AS a
WHERE   NAME = 'a'
        AND NOT EXISTS ( SELECT 1
                         FROM   t
                         WHERE  id = a.id
                                AND NAME = 'b' )