是否可以使用 MYSQL GROUP BY 选择堆栈值

发布于 2024-11-17 19:17:33 字数 1416 浏览 3 评论 0原文

我收到这样的查询:

SELECT email
FROM abc_table
GROUP BY email
HAVING ( COUNT(email) > 1 )

所以它会返回我:

email   
[email protected]     
[email protected]     
[email protected]

现在我需要调整查询以获得这样的结果:

email       id
[email protected]     1
[email protected]     2   
[email protected]     3
[email protected]     4
[email protected]     5
[email protected]     6

是否可以通过使用 GROUP BY HAVING 获得此结果?或者有什么建议可以得到这个结果吗?

多谢!

I got a query like this :

SELECT email
FROM abc_table
GROUP BY email
HAVING ( COUNT(email) > 1 )

So it will return me :

email   
[email protected]     
[email protected]     
[email protected]

And now i need to adjust the query to get something like this :

email       id
[email protected]     1
[email protected]     2   
[email protected]     3
[email protected]     4
[email protected]     5
[email protected]     6

Is it posible to get this result by using GROUP BY HAVING ? Or any suggestion to get this result?

Thanks a lot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦初启 2024-11-24 19:17:33
SELECT a.email
     , a.id
FROM abc_table a
  JOIN
    ( SELECT email
      FROM abc_table
      GROUP BY email
      HAVING COUNT(email) > 1 
    ) AS ag
    ON ag.email = a.email
SELECT a.email
     , a.id
FROM abc_table a
  JOIN
    ( SELECT email
      FROM abc_table
      GROUP BY email
      HAVING COUNT(email) > 1 
    ) AS ag
    ON ag.email = a.email
隔纱相望 2024-11-24 19:17:33

使用 GROUP_CONCAT:(

SELECT email, GROUP_CONCAT(id) FROM abc_table GROUP BY email

编辑:误读问题)

Use GROUP_CONCAT:

SELECT email, GROUP_CONCAT(id) FROM abc_table GROUP BY email

(EDIT: misread the question)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文