表saleorder
status customerid
4          1
5          1
4          2
1     3
2     4
4          5
5          6结果
customerid
2
5查询出所有status 为4不为5的customerid

解决方案 »

  1.   

    select customerid from saleorder where status='4' and status!-'5'
      

  2.   

    SELECT * FROM [Table] a WHERE 
    [STATUS]=4 AND customerid NOT IN(SELECT customerid FROM [Table] WHERE customerid=a.customerid AND [STATUS]=5)
      

  3.   

    SELECT * FROM saleorder a WHERE 
    STATUS='4' AND customerid NOT IN(SELECT customerid FROM saleorder b WHERE b.customerid=a.customerid AND STATUS='5')
      

  4.   

    select customerid from saleorder where [status]=4 and [status]<>5
      

  5.   

    select customerid
    from saleorder a 
    where STATUS='4' and customerid not in(select customerid from saleorder b where b.customerid=a.customerid and STATUS='5');或者:select customerid
    from saleorder  
    where STATUS='4'
    EXCEPT
    select customerid
    from saleorder  
    where STATUS='5';其中(EXCEPT表示差集)