[英]How to keep one row for each unique column value after groupby in MySQL?
I have a column of ids and a column of emails.我有一列 id 和一列电子邮件。 I would like to group by id and only keep rows with distinct emails (it can be any row).
我想按 id 分组,只保留具有不同电子邮件的行(可以是任何行)。 For example:
例如:
+-------------------------------+
| Id Email |
+-------------------------------+
| 1 email1 |
| 1 email2 |
| 1 email1 |
| 2 email3 |
| 2 email3 |
| 3 email4 |
| 3 email4 |
+-------------------------------+
The final output would be:最终输出将是:
+---------------------------+
| Id Email |
+---------------------------+
| 1 email1 |
| 1 email2 |
| 2 email3 |
| 3 email4 |
+---------------------------+
The idea is something like这个想法就像
SELECT * FROM table
GROUP BY Id
HAVING DISTINCT(email)
but the syntax is not correct.但语法不正确。
Any help would be appreciated.任何帮助,将不胜感激。 Thank you!
谢谢!
For this dataset, a simple select distinct
should do:对于这个数据集,一个简单的
select distinct
应该做:
select distinct id, email from mytable
This simply removes duplicate rows from the resultset, which seems to be what you are looking for.这只是从结果集中删除重复的行,这似乎是您要查找的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.