带有WHERE的DISTINCT子句

时间:2011-04-10 08:11:57

标签: sql distinct where

如何在WHERE中使用DISTINCT子句?例如:

SELECT * FROM table WHERE DISTINCT email; -- email is a column name

我想从具有不同电子邮件地址的表格中选择所有列。

12 个答案:

答案 0 :(得分:27)

如果您指的是其电子邮件唯一的所有列:

SELECT * FROM table WHERE email in
     (SELECT email FROM table GROUP BY email HAVING COUNT(email)=1);

答案 1 :(得分:3)

可能是:

SELECT DISTINCT email,id FROM table where id='2';

答案 2 :(得分:3)

select t1.*
from YourTable as t1
  inner join
    (select email
     from YourTable
     group by email
     having count(email) = 1 ) as t2
    on t1.email = t2.email   

答案 3 :(得分:2)

您可以使用HAVING子句。

SELECT * 
FROM tab_name
GROUP BY email_id
HAVING COUNT(*) = 1;

答案 4 :(得分:0)

您可以使用ROW_NUMBER()。您也可以指定条件。 (例如,以下查询中的Name LIKE'MyName%

SELECT  *
FROM    (SELECT ID, Name, Email,
            ROW_NUMBER() OVER (PARTITION BY Email ORDER BY ID) AS RowNumber
     FROM   MyTable
     WHERE  Name LIKE 'MyName%') AS a
WHERE   a.RowNumber = 1

答案 5 :(得分:0)

一个简单的查询就可以了:

SELECT * 
FROM table 
GROUP BY email 
HAVING COUNT(*) = 1;

答案 6 :(得分:0)

不会这样做:

 SELECT email FROM table1 t1 
          where UNIQUE(SELECT * FROM table1 t2); 

答案 7 :(得分:0)

简单查询,此查询从表中选择电子邮件唯一的所有记录:

select distinct email,* from table_name

答案 8 :(得分:0)

如果表中有唯一的列(例如tableid),请尝试此操作。

SELECT EMAIL FROM TABLE WHERE TABLEID IN 
(SELECT MAX(TABLEID), EMAIL FROM TABLE GROUP BY EMAIL)

答案 9 :(得分:0)

尝试:

SELECT * FROM table GROUP BY email

  • 这将返回所有行,并在第一个ID出现时使用唯一的电子邮件(如果可以的话)
  • 我认为这是您一直在寻找的东西,因为我有一个相同的问题,但是这些答案都不对我有用。

答案 10 :(得分:0)

SELECT DISTINCT dbo.Table.Email,dbo.Table.FirstName dbo.Table.LastName,dbo.Table.DateOfBirth(etc)from dbo.Table.Contacts 电子邮件='name @ email';

答案 11 :(得分:-1)

查询:

Select *, (Select distinct email) from Table1