MySQL 中部分匹配的字段分组
我试图返回用户表中的重复记录,其中字段仅部分匹配,并且匹配的字段内容是任意的。我不确定我是否解释得很好,所以这是我可能运行的查询,以通过某个唯一字段获取重复成员:
SELECT MAX(id)
FROM members
WHERE 1
GROUP BY some_unique_field
HAVING COUNT(some_unique_field) > 1
我想将相同的想法应用于电子邮件字段,但不幸的是我们的电子邮件字段可以包含多个电子邮件,以逗号分隔。例如,我希望成员的电子邮件设置为“[电子邮件受保护]”作为具有“[电子邮件受保护]","[电子邮件受保护]" 在其字段中。 GROUP BY
显然无法按原样完成此任务。
I'm trying to return duplicate records in a user table where the fields only partially match, and the matching field contents are arbitrary. I'm not sure if I'm explaining it well, so here is the query I might run to get the duplicate members by some unique field:
SELECT MAX(id)
FROM members
WHERE 1
GROUP BY some_unique_field
HAVING COUNT(some_unique_field) > 1
I want to apply this same idea to an email field, but unfortunately our email field can contain multiple e-mails seperated by a comma. For example, I want a member with his email set to "[email protected]" to be returned as a duplicate of another member that has "[email protected]","[email protected]" in their field. GROUP BY
obviously will not accomplish this as-is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这样的方法可能对您有用:
这取决于您的电子邮件地址在有多个地址时的格式一致性。如果逗号后面始终有空格,或者引号实际上是数据的一部分,您可能需要稍微修改查询。
Something like this might work for you:
It depends on how consistently your email addresses are formatted when there are more than one. You might need to modify the query slightly if there is always a space after the comma, e.g., or if the quotes are actually part of your data.
这对我有用;可能不会执行您想要的操作:
SELECT MAX(ID)
来自会员
WHERE 类似“%someuser%”的电子邮件
按电子邮件分组
已计数(电子邮件)> 1
This works for me; may not do what you want:
SELECT MAX(ID)
FROM members
WHERE Email like "%someuser%"
GROUP BY Email
HAVING COUNT(Email) > 1