Skip to content
Advertisement

Get Ids of rows that has duplicates in a column

I have the following table users

I need to get all ids, where users have duplicate phone. For example, the query must return the following dataset: [1,5]. Because user with id 1 has phone +1111 and user with id 5 has phone +1111.

There is something close in this answer. But I need to modify it for mysql. Currently, I have the following sql:

But it returns a response with not wery good format. See:

enter image description here

Of corse, the reason is group_concat.

So, I need to get result as list of ids, not list of groups. Ho to do that for mysql?

Advertisement

Answer

Of course, the Group Sum should be the preferred solution on MySQL 8, but this is a perfect case for a simple Subquery:

If you need to display the actual count, go with Tim’s solution.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement