如何在WHERE中使用DISTINCT子句?例如:與WHERE的DISTINCT子句
SELECT * FROM table WHERE DISTINCT email; -- email is a column name
我想選擇具有不同電子郵件地址的表中的所有列。
如何在WHERE中使用DISTINCT子句?例如:與WHERE的DISTINCT子句
SELECT * FROM table WHERE DISTINCT email; -- email is a column name
我想選擇具有不同電子郵件地址的表中的所有列。
如果你的意思是其電子郵件是獨一無二的所有列:
SELECT * FROM table WHERE email in
(SELECT email FROM table GROUP BY email HAVING COUNT(email)=1);
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
您可以使用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
一個簡單的查詢將做到這一點:
SELECT *
FROM table
GROUP BY email
HAVING COUNT(*) = 1;
該操作沒有指定SQL的風格,但該命令不會在SQL Server中運行,並在MySQL中產生不明確的結果。 – 2017-03-27 16:06:16
這是不是工作:
SELECT email FROM table1 t1
where UNIQUE(SELECT * FROM table1 t2);
我不知道有很多DBMS已經實現了UNIQUE謂詞。你在使用哪一個?另外,我認爲你向後查詢了:不應該將'SELECT email'包裝在'UNIQUE()'謂詞中?我認爲你需要某種連接子句('FROM table1 t2 WHERE t2.email = t1.email')。 – JDB 2016-08-20 03:45:27
查詢:
Select *, (Select distinct email) from Table1
您可以使用HAVING
條款。
SELECT *
FROM tab_name
GROUP BY email_id
HAVING COUNT(*) = 1;
這是矛盾的條款。你的意思是「選擇電子郵件是唯一的所有列」? – 2011-04-10 08:19:06
@adam: - 是的! – Mohit 2011-04-10 08:20:05
好的,回答了你的問題。 – 2011-04-10 08:21:00