求一条sql语句A,B,C三个表,  A表字段Aname ,B表字段 Bname,C表字段Cname要求的结果是查询记录,A表Aname,B,表Bname的值,Aname,Bname在同一列出现。
A中的Aname值不能等于C表中的Cname的值

解决方案 »

  1.   

    select distinct [name] from (
    select [Aname] from A where [Aname] not in(select [Cname] from C)
    union all select [Bname] from B )t
      

  2.   

    select Aname from A
    where exists(select 1 from B where Bname=a.Aname)
      and not exists(select 1 from C where Cname=a.Aname)
      

  3.   

    select A.Aname from A,C where Aname<>Cname
    union
    select Bname from B
      

  4.   


    select Aname from A
    where exists(select 1 from B where Bname=a.Aname)
      and not exists(select 1 from C where Cname=a.Aname)
      

  5.   


    select Aname from A where not exists(select 1 from C where Cname=a.Aname)
    union all
    select Bname from B
      

  6.   

    这种效率比较高,只遍历了一次A表。select Aname from A
    where exists(select 1 from B where Bname=a.Aname)
      and not exists(select 1 from C where Cname=a.Aname)