一、Select distinct [Email] from chinadat.chinadat.Members
Where [Email] not in (select [Email] from Email.dbo.Emails) and [Email] <> ''二、Select distinct [Email],[type] from chinadat.chinadat.Members
Where [Email] not in (select [Email] from Email.dbo.Emails) and [Email] <> ''解释一下,上面的SQL语句牵扯到2张个数据库,chinadat数据库的Members表和Email数据库的Emails表,这两条SQL语句唯一的区别就是第2条语句多查询了一个type字段,但是查询出来的结果数是不一样的,怎么修改第2条语句使其能和第一条语句有同样的效果?

解决方案 »

  1.   

    --TRY
    Select  [Email],[type] from chinadat.chinadat.Members t  where not exists(select 1 from chinadat.chinadat.Members where 
    [Email]=t.[Email] and [type]<t.[type] )
    and [Email] not in (select [Email] from Email.dbo.Emails) and [Email] <> '' 
      

  2.   

    distinct [Email],[type]只要这两个字段的内容组合起来不一样,就会出现在选择列表中.
    而只有一个字段的情况自然比这个要少.
      

  3.   

    这个问题很简单在一张表上查唯一的 [Email] 肯定是一样的问题是 你第二句话加了 [type] 那么 [Email]+[type] 唯一的 肯定比 [Email] 唯一的要多你不可能让第二个查询 跟第一个结果一样的除非在相同的[Email] 下定个规则取唯一的[type]
      

  4.   

    distinct [Email],[type]的意思是[Email],[type]两个字段的组合唯一Select [Email],min([type]) as [type] from chinadat.chinadat.Members 
    Where [Email] not in (select [Email] from Email.dbo.Emails) and [Email] <> '' 
    group by [Email]
      

  5.   

    Select distinct [Email] from chinadat.chinadat.Members 
    Where [Email] not in (select [Email] from Email.dbo.Emails) and [Email] <> '' Select [Email],max([type]) type from chinadat.chinadat.Members 
    Where [Email] not in (select [Email] from Email.dbo.Emails) and [Email] <> '' 
    group by [email]-- or
    Select [Email],min([type]) type from chinadat.chinadat.Members 
    Where [Email] not in (select [Email] from Email.dbo.Emails) and [Email] <> '' 
    group by [email]
      

  6.   

    多了一列
    [type]--查詢結查,同一個[Email]存在不同[type]時,會顯示多條查詢優化有可能會造成用到聚集索引
      

  7.   

    Select [Email],max([type]) as [type] from chinadat.chinadat.Members 
    Where [Email] not in (select [Email] from Email.dbo.Emails) and [Email] <> '' 
    group by [Email]