姓名  出差城市
张三  上海
张三  北京
李四  上海现在要找出没有出差去过 北京 的人,如何写sql语句?

解决方案 »

  1.   

    select * from tb t where not exists (select 1 from tb where 姓名=t.姓名 and 出差城市='北京')
      

  2.   

    select * from tb a where 姓名 not in
    (select 姓名 from tb where 出差城市='上海')
      

  3.   


    select * from tb a where 姓名 not in
    (select 姓名 from tb where 出差城市='北京') 
      

  4.   

    SELECT DISTINCT 姓名
    FROM TABLENAME A
    WHERE NOT EXISTS (
    SELECT 1 FROM TABLENAME B
    WHERE B.姓名 = A.姓名
    AND 出差城市 = '北京'
    )