![](/img/trans.png)
[英]Selecting two rows in a table which have the same data for a particular column
[英]SQL - Selecting data that is the same in multiple rows for a particular key
我有一個如下的數據集:
KEY USER STEP
---------------------
123 Geoff 1
123 Geoff 2
123 John 3
456 Jane 1
456 Jane 2
456 Jane 3
456 Jane 4
我需要做的是選擇所有步驟中USER都相同的KEY(數據集中的1-5個步驟之間)。 因此,在上面的示例中,我需要查詢返回KEY456。非常感謝您的幫助!
您可以使用GROUP BY
和HAVING
來做到這一點:
select key
from table t
group by key
having min(user) = max(user);
注意: key
和user
都是關鍵字。 您可能必須使用反引號(或者希望您的實際列名有所不同)。
您也可以執行以下max(user)
,而不是與min(user)
和max(user)
進行比較:
SELECT tablename.key
FROM tablename
GROUP BY tablename.key HAVING count(DISTINCT d.user) = 1;
count(DISTINCT d.user)
應該對您的用例更具描述性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.