2015-12-15 141 views
0

我有一個名爲'emails'的列表。我需要一個查詢來選擇只在表中出現一次的電子郵件。MySql:選擇唯一值

所以當'emails'列有([email protected][email protected][email protected]) 我只想選擇[email protected]

我曾嘗試以下查詢:

SELECT DISTINCT `emails` FROM `table` 

但問題是,它選擇[email protected][email protected]

我需要一個查詢什麼只會選擇[email protected]

任何人都可以幫助我嗎?

+3

'SELECT COUNT(*)作爲CNT,電子郵件從表組通過電子郵件有cnt = 1' –

+1

'通過電子郵件從表組中選擇電子郵件計數(*)= 1' –

+0

@AbhikChakraborty完美的作品!謝謝!你不應該創建一個我可以接受的答案嗎? – SiteHopper

回答

1

您可以使用以下

select 
count(*) as cnt, 
email from table 
group by email having cnt = 1 

如果你有一個自增的主鍵,然後你有其他的方法,以及一些如

select * from table t1 
where not exists (
select 1 from table t2 
where t1.email = t2.email 
and t1.id <> t2.id 
);